src.c:
main()
{
char c='x';
read(0,&c,1);
printf("go\n");
printf("come\n");
}
gcc src.c -g,你得到一个可执行文件a.out,然后, gdb a.out
(gdb) b main
Breakpoint 1 at 0x80483e5: file gdb.c, line 3.
(gdb) r
Starting program: /tmp/tx
Breakpoint 1, main () at gdb.c:3
3 char c='x';
(gdb) set $foo=&c
(gdb) watch *$foo //set a watchpoint for the address in which c reside
Hardware watchpoint 2: *$foo
(gdb) del 1
(gdb) c
Continuing.
Hardware watchpoint 2: *$foo
Old value = 0 '\0'
New value = 120 'x'
main () at gdb.c:4
4 read(0,&c,1);
(gdb) c
Continuing. **//type carrige return**
Hardware watchpoint 2: *$foo
Old value = 120 'x'
New value = 10 '\n'
0x00ae3402 in __kernel_vsyscall ()
(gdb)
Continuing.
Program received signal SIGTRAP, Trace/breakpoint trap. **//from now on,gdb receive SIGTRAP repeatedly,why?**
0x009f23f3 in __read_nocancel () from /lib/libc.so.6
(gdb)
Continuing.
Program received signal SIGTRAP, Trace/breakpoint trap.
0x009f23f4 in __read_nocancel () from /lib/libc.so.6
(gdb)
Continuing.
Program received signal SIGTRAP, Trace/breakpoint trap.
0x009f23f9 in __read_nocancel () from /lib/libc.so.6
(gdb)
但是,如果我将源代码修改为:
main()
{
char c='x';
printf("go\n");
c++;
printf("come\n");
}
然后一切顺利,没有有线行为。
在第一个程序中,watchpoint的值在内核模式下被修改,然后gdb接收SIGTRAP;在第二个程序中,watchpoint在用户模式下被修改,没有任何连接发生。有人告诉我为什么?提前谢谢。