gdb git:(master) ✗ gdb a.out GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git Copyright (C) 2021 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-linux-gnu". Type "show configuration"for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>.
For help, type"help". Type "apropos word" to search for commands related to "word"... Reading symbols from a.out... (gdb) r Starting program: /home/hoo/Projects/codeBase/base/cpp/gdb/a.out mom mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm^C Program received signal SIGINT, Interrupt. 0x00007ffff7ee4f33 in __GI___libc_write (fd=1, buf=0x5555555596b0, nbytes=1024) at ../sysdeps/unix/sysv/linux/write.c:26 26 ../sysdeps/unix/sysv/linux/write.c: No such file or directory. (gdb)
程序运行后出现无限循环的问题,使用 Ctrl + C 向程序发送 SIGHT。GDB 将会捕获这个信号并停止程序。
现在我们使用backtrace命令查看运行栈。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(gdb) bt #0 0x00007ffff7ee4f33 in __GI___libc_write (fd=1, buf=0x5555555596b0, nbytes=1024) at ../sysdeps/unix/sysv/linux/write.c:26 #1 0x00007ffff7e76665 in _IO_new_file_write (f=0x7ffff7fb56a0 <_IO_2_1_stdout_>, data=0x5555555596b0, n=1024) at fileops.c:1181 #2 0x00007ffff7e759d6 in new_do_write (fp=fp@entry=0x7ffff7fb56a0 <_IO_2_1_stdout_>, data=0x5555555596b0 'm' <repeats 200 times>..., to_do=to_do@entry=1024) at libioP.h:948 #3 0x00007ffff7e77709 in _IO_new_do_write (to_do=1024, data=<optimized out>, fp=0x7ffff7fb56a0 <_IO_2_1_stdout_>) at fileops.c:423 #4 _IO_new_do_write (fp=fp@entry=0x7ffff7fb56a0 <_IO_2_1_stdout_>, data=<optimized out>, to_do=1024) at fileops.c:423 #5 0x00007ffff7e77bdf in _IO_new_file_overflow (f=0x7ffff7fb56a0 <_IO_2_1_stdout_>, ch=109) at fileops.c:779 #6 0x00007ffff7e6e2ae in putchar (c=109) at putchar.c:28 #7 0x00005555555551a3 in main (argc=1, argv=0x7fffffffe338) at 1.c:10
(gdb) next mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm_IO_new_file_write (f=0x7ffff7fb56a0 <_IO_2_1_stdout_>, data=0x5555555596b0, n=1024) at fileops.c:1182 1182 fileops.c: No such file or directory. (gdb) next _IO_new_do_write (fp=fp@entry=0x7ffff7fb56a0 <_IO_2_1_stdout_>, data=<optimized out>, to_do=<optimized out>) at fileops.c:423 423 in fileops.c (gdb) next _IO_new_file_overflow (f=0x7ffff7fb56a0 <_IO_2_1_stdout_>, ch=109) at fileops.c:781 781 in fileops.c (gdb) next 782 in fileops.c (gdb) next 787 in fileops.c (gdb) next putchar (c=109) at putchar.c:27 27 putchar.c: No such file or directory. (gdb) next main (argc=1, argv=0x7fffffffe338) at 1.c:8 8 while (c != EOF) {
现在我们已经进入到 main 函数,使用 next 命令查看程序如何执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(gdb) next 9 if (isalnum(c)) (gdb) next 10 printf("%c", c); (gdb) 8 while (c != EOF) { (gdb) next 9 if (isalnum(c)) (gdb) next 10 printf("%c", c); (gdb) next 8 while (c != EOF) { (gdb) next 9 if (isalnum(c)) (gdb) next 10 printf("%c", c);
可以看到这几行代码重复执行,其中 c 的值并没有改变一直是 m。所以肯能是这一段程序出现问题:
1 2 3 4
if (isalnum(c)) printf("%c", c); else c = fgetc(stdin);
gcc -g 2.c 2.c: In function ‘main’: 2.c:7:11: warning: argument 1 value ‘18446744071562067968’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=] 7 | buf = malloc(1 << 31); | ^~~~~~~~~~~~~~~ In file included from 2.c:2: /usr/include/stdlib.h:539:14: note: in a call to allocation function ‘malloc’ declared here 539 | extern void *malloc (size_t __size) __THROW __attribute_malloc__ | ^~~~~~
gdb a.out GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git Copyright (C) 2021 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-linux-gnu". Type "show configuration"for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>.
For help, type"help". Type "apropos word" to search for commands related to "word"... Reading symbols from a.out... (gdb) r Starting program: /home/hoo/Projects/codeBase/base/cpp/gdb/a.out st
Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7e6ba71 in __GI__IO_getline_info ( fp=fp@entry=0x7ffff7fb4980 <_IO_2_1_stdin_>, buf=buf@entry=0x0, n=n@entry=1023, delim=delim@entry=10, extract_delim=extract_delim@entry=1, eof=eof@entry=0x0) at iogetline.c:77 77 iogetline.c: No such file or directory.
使用命令查看运行栈。
1 2 3 4 5 6 7 8 9 10 11
(gdb) bt #0 0x00007ffff7e6ba71 in __GI__IO_getline_info ( fp=fp@entry=0x7ffff7fb4980 <_IO_2_1_stdin_>, buf=buf@entry=0x0, n=n@entry=1023, delim=delim@entry=10, extract_delim=extract_delim@entry=1, eof=eof@entry=0x0) at iogetline.c:77 #1 0x00007ffff7e6bb58 in __GI__IO_getline ( fp=fp@entry=0x7ffff7fb4980 <_IO_2_1_stdin_>, buf=buf@entry=0x0, n=n@entry=1023, delim=delim@entry=10, extract_delim=extract_delim@entry=1) at iogetline.c:34 #2 0x00007ffff7e6aa56 in _IO_fgets (buf=0x0, n=1024, fp=0x7ffff7fb4980 <_IO_2_1_stdin_>) at iofgets.c:53 #3 0x000055555555518c in main (argc=1, argv=0x7fffffffe338) at 2.c:9
查看 frame 3 中的代码,到底是哪里出现问题。
1 2 3
(gdb) frame 3 #3 0x000055555555518c in main (argc=1, argv=0x7fffffffe338) at 2.c:9 9 fgets(buf, 1024, stdin);