2016-12-02 21 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