watch -- 监视变量值
源程序:
/********************************************************
include
*******************************************************/
#include <stdio.h>
#include <string.h>
/*******************************************************
define
*******************************************************/
#define MAX_NAME_LEN (15)
typedef unsigned char BYTE_T;
typedef int INT32_T;
typedef unsigned int UINT32_T;
INT32_T g_i32WatchNum = 0;
BYTE_T g_pu8Str[15] = "global char";
/********************************************************/
int main(int argc, char *argv[])
{
printf("%s\n", g_pu8Str);
printf("start watch valiable\n");
memcpy(g_pu8Str, "ppp\n", sizeof("ppp\n"));
printf("%s\n", g_pu8Str);
memcpy(g_pu8Str, "bbb\n", sizeof("bbb\n"));
printf("geb_test\n");
getchar();
}
编译:
gcc -g ./gdb_test.c
调试:
root@ubuntu:/home/commander/project/gdb_test# clear
root@ubuntu:/home/commander/project/gdb_test# gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/commander/project/gdb_test/a.out...done.
(gdb) watch g_pu8Str
Watchpoint 1: g_pu8Str
(gdb) r
Starting program: /home/commander/project/gdb_test/a.out
global char
start watch valiable
Watchpoint 1: g_pu8Str
Old value = "global char\000\000\000" -------------- 断点触发,旧值global char
New value = "ppp\nal char\000\000\000" -------------- 断点触发后,更新的值"ppp\nal char\0
0x08048443 in main (argc=1, argv=0xbffff734) at ./gdb_test.c:26 -------------- 断点于 gdb_test.c文件26行
26 memcpy(g_pu8Str, "ppp\n", sizeof("ppp\n"));
(gdb)
(gdb)
(gdb)
(gdb) c -------------- 继续执行 continue
Continuing.
Watchpoint 1: g_pu8Str ---------------再次中断于第一个监视的变量,访问触发 watchpoint 1:g_pu8Str
Old value = "ppp\nal char\000\000\000"
New value = "ppp\n\000l char\000\000\000"
main (argc=1, argv=0xbffff734) at ./gdb_test.c:27
27 printf("%s\n", g_pu8Str);
(gdb) ----------------直接回车,相当于继续输入continue命令
Continuing.
ppp
Watchpoint 1: g_pu8Str
Old value = "ppp\n\000l char\000\000\000" ----------------g_pu8Str变量值更改,断点
New value = "bbb\n\000l char\000\000\000"
0x08048464 in main (argc=1, argv=0xbffff734) at ./gdb_test.c:28
28 memcpy(g_pu8Str, "bbb\n", sizeof("bbb\n"));
(gdb)
Continuing.
geb_test
[Inferior 1 (process 2981) exited with code 012]
(gdb) l ----------------源代码列出
23 {
24 printf("%s\n", g_pu8Str);
25 printf("start watch valiable\n");
26 memcpy(g_pu8Str, "ppp\n", sizeof("ppp\n"));
27 printf("%s\n", g_pu8Str);
28 memcpy(g_pu8Str, "bbb\n", sizeof("bbb\n"));
29 printf("geb_test\n");
30 getchar();
31 }
(gdb)