GNU gdb (Debian 8.3.1-1) 8.3.1 ... ... Type "apropos word" to search for commands related to "word"... Reading symbols from watch... (gdb) b main # 设置普通断点在main函数 Breakpoint 1 at 0x113d: file watch.c, line 6. # 断点编号为1 (gdb) b watch.c:7 if i == 8 # 设置条件断点在第七行如果i==8 Breakpoint 2 at 0x1146: file watch.c, line 7. # 断点编号为2 (gdb) run # 运行被调试程序 Starting program: /home/f/doing/debug/gdb/watch
Breakpoint 1, main () at watch.c:6 # 执行到断点1后停下来 6 for (i = 0; i < 10; i++) { (gdb) continue# continue是继续执行 Continuing. 0 1 2 3 4 5 6 7
Breakpoint 2, main () at watch.c:7 # 断点2满足条件,停下来 7 printf("%d\n", i); (gdb) print i # print 查看变量值,可以看到等于8,满足我们的条件 $1 = 8
GNU gdb (Debian 8.3.1-1) 8.3.1 ... ... Reading symbols from watch... (gdb) b main # 要监控变量i,由于i是在main方法中,首先要运行到main函数上下文 Breakpoint 1 at 0x113d: file watch.c, line 6. (gdb) run # 执行被调试程序 Starting program: /home/f/doing/debug/gdb/watch
Breakpoint 1, main () at watch.c:6 # 执行到main函数处中断 6 for (i = 0; i < 10; i++) { (gdb) watch i if i == 8 # 监控变量i,并让i==8时断下来 Hardware watchpoint 2: i (gdb) c Continuing. 0 1 2 3 4 5 6 7
Hardware watchpoint 2: i
Old value = 7 New value = 8 0x0000555555555160 in main () at watch.c:6 # i从7更改8被断下来 6 for (i = 0; i < 10; i++) {
GNU gdb (Debian 8.3.1-1) 8.3.1 ... ... Reading symbols from ./watch... (gdb) b main Breakpoint 1 at 0x113d: file watch.c, line 6. (gdb) run Starting program: /home/f/doing/debug/gdb/watch
Breakpoint 1, main () at watch.c:6 6 for (i = 0; i < 10; i++) { (gdb) catch syscall write # 事件断点在write系统调用上 Catchpoint 2 (syscall 'write' [1]) (gdb) continue Continuing.
Catchpoint 2 (call to syscall write), 0x00007ffff7ec8904 in __GI___libc_write (fd=1, buf=0x5555555592a0, nbytes=2) at ../sysdeps/unix/sysv/linux/write.c:26 26 ../sysdeps/unix/sysv/linux/write.c: No such file or directory. (gdb) x/2cb 0x5555555592a0 # 根据write的参数,buf和nbytes,我们查看下要打印的内容 0x5555555592a0: 48 '0' 10 '\n'# 打印内容为 0和换行符 (gdb) continue Continuing. 0
断点管理
1 2 3 4 5 6 7
(gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x000055555555513d in main at watch.c:6 breakpoint already hit 1 time 2 hw watchpoint keep y i 3 catchpoint keep y syscall "write" 4 breakpoint del y 0x000055555555513d in main at watch.c:6
GNU gdb (Debian 8.3.1-1) 8.3.1 ... ... Reading symbols from ./watch... (gdb) b watch.c:7 # 断点在printf行 Breakpoint 1 at 0x1146: file watch.c, line 7. (gdb) commands # 绑定命令序列到该断点上 Type commands for breakpoint(s) 1, one per line. End with a line saying just "end". >printf"%d\n", i+1000 # 输出i+1000 >continue# 继续执行 >end # 命令序列完成关键字 (gdb) run Starting program: /home/f/doing/debug/gdb/watch
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1000 0
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1001 1
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1002 2
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1003 3
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1004 4
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1005 5
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1006 6
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1007 7
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1008 8
Breakpoint 1, main () at watch.c:7 7 printf("%d\n", i); 1009 9 [Inferior 1 (process 128987) exited normally]