当前位置: > Linux编程 >

CentOS 下利用 core 文件调试程序

时间:2015-01-10 20:11来源:linux.it.net.cn 作者:IT
1:设置系统允许生产core文件
在 ~/.bash_profile 中增加
ulimit -c unlimited
 
使用ulimit -a可以查看系统core文件的大小限制;
使用ulimit -c [kbytes]可以设置系统允许生成的core文件大小;
ulimit -c 0 不产生core文件
ulimit -c 100 设置core文件最大为100k
ulimit -c unlimited 不限制core文件大小
 
2:利用 core 文件调试简单例子
  1.  
  2. #include <stdio.h>
  3. int
  4. main (int argc, char **argv)
  5. {
  6.     char *p="test message!";
  7.     *p=0;
  8.     return 0;
  9. }
 
编译运行后结果如下:
 
$ gcc -g -o test test.c
 
$ ./test
Segmentation fault (core dumped)
 
$ ls core.*
core.10747
 
core文件已经生成,接下来可以用gdb分析
 
$gdb ./test core.10747
 
GNU gdb 6.8
Copyright (C) 2008 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-unknown-linux-gnu"...
 
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib64/libc.so.6...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
 
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff1b15e000
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
[New process 10747]
#0  0x000000000040045f in main (argc=1, argv=0x7fff1b0cb6f8) at test.c:7
7 *p=0;
 
可以看到问题出在第7行
 
3:更详细信息, 参考 man core
(责任编辑:IT)
------分隔线----------------------------