解決Linux c語言運行時候“段錯誤 (核心已轉儲)”問題-采用gdb 解決


編譯沒有警告,沒有錯誤,運行就打印 段錯誤 (核心已轉儲)

網上找了一下,都是各種問題,都推薦用gdb 調試解決,咱也來趁機學習gdb一下。
 

gcc+gdb)輸入命令行 運行

sudo apt-get install build-essential

build-essential包含gcc和gdb等工具,是C語言的開發包。

安裝完了可以執行

 

一般來說GDB主要調試的是C/C++的程序。要調試C/C
++的程序,首先在編譯時,我們必須要把調試信息加到可執行文件中。使用編譯器(cc/
gcc/g++)的 -g 參數可以做到這一點。

 

進程意外退出會在當前目錄下產生‘code’文件或形如‘core.數字’的文件比如‘core.1234’
使用命令
gdb 運行程序名 core或core.數字
進入gdb然后使用bt命令
可以查看進程意外退出前函數調用的堆棧,內容為從上到下列出對應從里層到外層的函數調用歷史。
如果進程意外退出不產生core文件

運行 ulimit -c unlimited   后再運行編譯出的可執行程序,就可以產生core 文件

gdb <program> core
用gdb同時調試一個運行程序和core文件,core是程序非法執行後core dump
後產生的文件。

然后再使用bt 命令就可以看出程序問題在哪里了,gdb真是好東西。

gdb a.out core
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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:
< 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...done.
[New LWP 1537]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  _IO_fgets (buf=0x7ffedcceef10 "", n=256, fp=0x0) at iofgets.c:50
50      iofgets.c: 沒有那個文件或目錄.
(gdb) bt
#0  _IO_fgets (buf=0x7ffedcceef10 "", n=256, fp=0x0) at iofgets.c:50
#1  0x000000000040198a in load_properties (path=0x401ad2 "/system/etc/tdgnss.conf") at rwfile.c:519
#2  0x00000000004019d9 in main () at rwfile.c:548

 

原來是不存在這個路徑/system/etc/tdgnss.conf ,這個路徑是以前Android上的,現在在Ubuntu 就不存在了,改一下估計就ok 了。

 

在Linux 上gdb 調試程序很方便,如果在arm 上怎么辦呢?尤其是我這種沒有usb 也沒有網卡的Linux arm 機器上?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM