2016-12-02 52 views
-4

我正在尝试在可执行文件使用的c文件中发现分段错误,但未找到任何线索。有谁知道如何做到这一点?如何使用gdb在.exe文件中查找分段错误

+1

您是否真的在整个网页上找不到有关使用gdb进行调试的问题? – kaylum

+0

@kaylum这就像战士俱乐部 - 找到你需要的东西,你应该知道你需要什么。而现代搜索引擎对新话题不利。他们试图通过相关性推动新事物的发展。好的指导https://beej.us/guide/bggdb/ – Swift

+0

我找不到任何东西。从Swift给我的链接中,我想我正在针对我的搜索查询进行特定搜索,并且没有足够的搜索范围。 – user3304124

回答

0

运行gdb并从gdb运行你的程序,然后使用回溯。你会得到堆栈框架,你可以通过fram命令来查看堆栈框架,并使用print来检查变量的值。通过互联网检查gdb tips \ docs。您可以使用gdb加载已崩溃的程序生成的已存在的核心文件,以查找发生问题的位置。加载的核心文件等于崩溃点的状态,您只需使用backtrace。

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

2

下面是一个简单的程序,肯定会导致段故障:

包括

int main() { 
    int *pVal = NULL; 

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

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

gcc -g segFault.c 

然后,您只需运行gdb并为其指定可执行文件路径(即本例中为a.out)。然后通过运行它可以看到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

快乐编码!

+0

有* nix Dropbox?整齐! – Swift