Chinaunix首页 | 论坛 | 博客
  • 博客访问: 583000
  • 博文数量: 109
  • 博客积分: 1463
  • 博客等级: 上尉
  • 技术积分: 859
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-22 13:21
个人简介

希望和广大热爱技术的童鞋一起交流,成长。

文章分类

全部博文(109)

文章存档

2017年(1)

2016年(2)

2015年(18)

2014年(1)

2013年(9)

2012年(15)

2011年(63)

分类: LINUX

2013-02-17 15:13:40

From:http://haobing2005.blog.163.com/blog/static/466678620097181052322/

1. core dump的含义
       core dump又叫核心转储。当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump。

2. core文件的名称和生成路径
       若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。
1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。

文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
$ echo "1" > /proc/sys/kernel/core_uses_pid
2)/proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
$ echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
       可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g - insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名

3. 为什么没有core文件生成
       有时候程序down了,但是core文件却没有生成,core文件的生成跟你当前的环境设置有关系。

可以用以下的语句设置一下,然后再运行程序便生成了.core文件。
        $ ulimit -c unlimited
       core文件生成位置一般与运行程序的路径相同,文件名一般为core进程。

4. 用gdb查看core文件
       发生core dump之后,用gdb进行查看core文件的内容,以定位文件中引发core dump的行。
       格式:gdb [exec file] [corefile]
如:
$ gdb ./test test.core
       在进入gdb后,用bt命令查看backtrace以检查发生程序运行到哪里。
$ gdb -c core 文件路径[应用程序路径]
       进去之后输入where回车,就可以显示程序在哪一行当掉的,在那个函数中。
       用gdb调试实例:
$ cat main.c

#include  
#include  
#include "mylib.h" 
int main (void) 
{      
    int ret = -1;      
    int a = 10, b = 20;     
    ret = add(a, b);         
    printf("The result is: %d\n", ret);   
    return 0; 
}

$ cat Makefile  
CC = gcc 
LD = gcc 
all:   
$(CC) mylib.c -g -I. -fPIC -shared -o libmylib.so   
$(CC) main.c -g -I. -L. -lmylib -o test 
clean:   
rm *.so test
$ cat mylib.c  
#include  
#include "mylib.h" 
int add(int x, int y) 
{      
    char* pc = NULL;   
    int i=10;      
    pc = &i;      
    return x + y; 
} 
$ cat mylib.h  
#ifndef __MY_LIB_H__ 
#define __MY_LIB_H__ 
int add(int x, int y); 
#endif //__MY_LIB_H__ 
$ make
gcc mylib.c -g -I. -fPIC -shared -o libmylib.so
gcc main.c -g -I. -L. -lmylib -o test
$ ./test 
./test: error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory
    这个错误表明程序在运行阶段不能找到相应的动态库文件,此时需要通过环境变量 LD_LIBRARY_PATH 来指定运行期动态库的搜索目录,我们的动态库就在当前目录,如下:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
$ ls
libmylib.so  main.c  Makefile  mylib.c  mylib.h  test
    系统在默认情况下core文件的大小设置为0,换句话讲也就是不产生core文件。我们可以通过 ulimit 命令来修改core文件的大小,unlimited表示不限制core文件的大小,如下:
$ ulimit -c unlimited
$ ./test 
Segmentation fault (core dumped)
$ ls
core  libmylib.so  main.c  Makefile  mylib.c  mylib.h  test
    用gdb进行查看
$  gdb test core
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0  0xb77f737c in add (x=10, y=20) at mylib.c:7
7     *pc = 10;
    错误发生在mylib.c的第7行,即*pc=10,pc的值未初始化。


阅读(3921) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~