错误代码
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char argv[]) { for (int i = 0; i < argc; ++i) { printf("%s\n", argv[i]); } return 0; }
编译运行结果如下
[zhoumengkang@localhost unix]$ gcc test.c -std=c99 [zhoumengkang@localhost unix]$ ./a.out 段错误 (core dumped)
学习ddb
的调试使用
[zhoumengkang@localhost unix]$ gcc -g test.c -std=c99 [zhoumengkang@localhost unix]$ gdb a.out GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) Copyright (C) 2010 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 "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /media/psf/coding/unix/a.out...done. (gdb) run Starting program: /media/psf/coding/unix/a.out Program received signal SIGSEGV, Segmentation fault. 0x0000003c9a04812c in vfprintf () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb) bt #0 0x0000003c9a04812c in vfprintf () from /lib64/libc.so.6 #1 0x0000003c9a04f48a in printf () from /lib64/libc.so.6 #2 0x00000000004004ff in main (argc=1, argv=0x7fffffffe4b8 "\364\346\377\377\377\177") at test.c:9 (gdb) frame 2 #2 0x00000000004004ff in main (argc=1, argv=0x7fffffffe4b8 "\364\346\377\377\377\177") at test.c:9 9 printf("%s\n", argv[i]); (gdb) break test.c:8 Breakpoint 1 at 0x4004dc: file test.c, line 8. (gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /media/psf/coding/unix/a.out Breakpoint 1, main (argc=1, argv=0x7fffffffe4b8 "\364\346\377\377\377\177") at test.c:9 9 printf("%s\n", argv[i]); (gdb) print argv[i] $2 = -12 '\364' (gdb) next Program received signal SIGSEGV, Segmentation fault. 0x0000003c9a04812c in vfprintf () from /lib64/libc.so.6 (gdb)