如何使用 gdb 查找 .exe 文件中的分段错误

How do I use gdb to find Segmentation Faults in a .exe file

我试图在可执行文件使用的 c 文件中查找分段错误,但我没有找到任何线索。有人知道怎么做吗?

运行 gdb 和 运行 来自 gdb 的程序,然后使用回溯。你 'll get stack frame, which you can walk through by fram command and use print to check variables' 价值观。通过互联网检查 gdb tips\docs。您可以使用 gdb 加载由崩溃程序生成的现有核心文件,以查找发生问题的位置。加载的核心文件等于崩溃点的状态,你只需使用回溯。

您的问题的答案在这里:Determine the line of C code that causes a segmentation fault?

这是一个肯定会导致段错误的示例程序:

包括

int main() {
    int *pVal = NULL;

    printf("ptr value is : %d", *pVal);
    return 0;
}

您需要在调试模式下编译才能在可执行文件中添加额外的调试信息:

gcc -g segFault.c

然后你只需要 运行 gdb 并给出可执行文件路径(即本例中的 a.out)。然后通过 运行ning 你可以看到 gdb 突出显示了导致分段错误的行。

 ~/Dropbox/cprog/demos : $ gdb a.out
GNU gdb (GDB) 7.12
Copyright (C) 2016 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-apple-darwin15.6.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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...Reading symbols from /Users/rohankumar/Dropbox/cprog/demos/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) run
Starting program: /Users/rohankumar/Dropbox/cprog/demos/a.out

Program received signal SIGSEGV, Segmentation fault.
0x0000000100000f62 in main () at segFault.c:6
6       printf("ptr value is : %d", *pVal);

您还可以打印这些值并查看程序的堆栈跟踪。你可以阅读更多关于 gdb here .

编码愉快!