如果我将崩溃程序和核心转储加载到gdb
,它会向我显示堆栈跟踪和崩溃点,如下所示。
Core was generated by `./cut --output-d=: -b1,1234567890- /dev/fd/63'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 is_printable_field (i=1234567890) at src/cut.c:266
266 return (printable_field[n] >> (i % CHAR_BIT)) & 1;
(gdb) bt
#0 is_printable_field (i=1234567890) at src/cut.c:266
#1 set_fields (fieldstr=0x7ffccb0561c4 "") at src/cut.c:533
#2 main (argc=4, argv=0x7ffccb055cf8) at src/cut.c:865
有没有办法知道导致segfault的确切汇编指令?
答案 0 :(得分:6)
一种可能性是设置:
(gdb)layout asm
当GDB停止时,指向相应的装配线。
示例:
│0x7ffff7aa441d <strtok+45> je 0x7ffff7aa44d6 <strtok+230> │
│0x7ffff7aa4423 <strtok+51> mov %rsi,%rax │
>│0x7ffff7aa4426 <strtok+54> mov (%rax),%cl │
│0x7ffff7aa4428 <strtok+56> test %cl,%cl │
│0x7ffff7aa442a <strtok+58> je 0x7ffff7aa4454 <strtok+100>
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7aa4426 in strtok () from /lib64/libc.so.6
(gdb)
答案 1 :(得分:3)
您可以使用disassemble
GDB命令。也可以在$rip
(x86-64上的程序计数器)
x/i
但是,在您的情况下,假设代码是C(不是带有operator []
的C ++),唯一可能的罪魁祸首是printable_field
指针或n
索引。 / p>
还考虑使用valgrind和/或使用GCC选项(最近的-fsanitize=
...编译器)编译(除了-g -Wall
选项),特别是-fsanitize=address
或-fsanitize=undefined
...