红联Linux门户
Linux帮助

解决Linux c语言运行时候“段错误 (核心已转储)”问题

发布时间:2016-06-13 10:35:48来源:linux网站作者:简单并快乐着

编译没有警告,没有错误,运行就打印 段错误 (核心已转储)
网上找了一下,都是各种问题,都推荐用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) 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-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 了。


本文永久更新地址:http://www.linuxdiyf.com/linux/21479.html