$ gcc -g -o segerr segerr.c --加-g选项查看调试信息 $ gdb ./segerr (gdb) l --用l(list)显示我们的源代码 1 #i nclude 2 3 int 4 main() 5 { 6 int i = 0; 7 8 scanf ("%d", i); 9 printf ("%d\n", i); 10 return 0; (gdb) b 8 --用b(break)设置断点 Breakpoint 1 at 0x80483b7: file segerr.c, line 8. (gdb) p i --用p(print)打印变量i的值[看到没,这里i的值是0哦] $1 = 0 (gdb) r --用r(run)运行,直到断点处 Starting program: /home/falcon/temp/segerr Breakpoint 1, main () at segerr.c:8 8 scanf ("%d", i); --[试图往地址0处写进一个值] (gdb) n --用n(next)执行下一步 10 Program received signal SIGSEGV, Segmentation fault. 0xb7e9a1ca in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6 (gdb) c --在上面我们接收到了SIGSEGV,然后用c(continue)继续执行 Continuing. Program terminated with signal SIGSEGV, Segmentation fault. The program no longer exists. (gdb) quit --退出gdb
|