本文由以下文章整理而来,向作者致谢。
http://blog.chinaunix.net/space.php?uid=9025392&do=blog&cuid=506966
http://www.cnblogs.com/sjpisaboy/articles/210228.html
http://sheng.iteye.com/blog/805406
1. core文件的简单介绍
Coredump在unix平台是非常容易出现的一种错误形式,直接表现形式为core文件。core文件以指定名称,产生在制定目录。通常,象内存地址错误、非法指令、总线错误等会引起coredump,core文件的内容包含进程出现异常时的错误影像,可以经core文件理解为崩溃时程序的内存映像(同时加上调试信息)。core文件可以用于调试追踪。如果错误进程为多线程并且core文件的大小受限于ulimit的系统限制,则系统只将数据区中错误线程的堆栈区复制到core文件中。
2. 开启或关闭core文件的生成
用以下命令来阻止系统生成core文件:ulimit -c 0
下面的命令可以检查生成core文件的选项是否打开:
ulimit -a 该命令将显示所有的用户定制,其中选项-a代表“all”。或者
ulimit -c 单独查看core-file的大小。
如果错误进程为多线程并且core文件的大小受限于ulimit的系统限制,则系统只将数据区中错误线程的堆栈区复制到core文件中。
也可以修改系统文件来调整core选项
在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置
在用户的~/.bash_profile里加上ulimit -c unlimited来让特定的用户可以产生core文件
???==》Q1:学习/etc/profile 和 ~/.bash_profile
3. 设置core file的大小:
用命令或者Linux API 可以设置core file的大小:
ulimit -c size(size的单位为blocks),或者
setrlimit()函数和RLIMIT_CORE选项。详见:http://blog.csdn.net/ispcfs/article/details/6844408
4. 设置Core Dump的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0
/proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e
可以这样修改:
echo "/corefile/core-%e-%p-%t" > core_pattern
将会控制所产生的core文件会存放到/corefile目录下,产生的文件名为core-命令名-pid-时间戳。
注意:修改/proc/sys/kernel/core_pattern需要权限。
相关参数列表:
%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 添加命令名
%% - %
除了这些参数之外,还规定:
a.末尾的单个%可以直接去除。
b.%加上除上述之外的任何字符,%和该字符都被去除。
c.所有其他字符都作为一般字符加入名称中。
d.core file的名称最大值为64字节(包括\0)。
e.core_pattern中的默认pattern为core。
f.为了保持兼容性,通过设置core_uses_pid,可以在core file名称的末尾加上%p。
g.pattern中可以包含路径信息。
注意:在AIX系统上:
从AIX 5L版本5.1开始core文件的命名格式可以通过环境变量CORE_NAMING设置,其格式为:core.pid.ddhhmmss,
分别代表为:
pid:进程标示符
dd:当前日期
hh:当前小时
mm:当前的分钟
ss:当前的秒
5. 使用core文件
利用gdb了解程序崩溃时,core file 记录的信息:
gdb -c core
它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等
如果已知是是何程序生成此core文件,比如MyServer崩溃了生成core.12345,那么用此指令调试:
gdb -c core MyServer
6. 一个小方法来测试产生core文件
kill -s SIGSEGV $$
7.core file 结构???Q2:==>> may be /usr/include/sys/core.h
core文件的缺省格式为老版本的格式,coredump文件的内容按照以下的顺序组织:???Q3:进一步学习core file结构
1) core文件的头部信息
定义coredump的基本信息,及其他信息的地址偏移量
2) ldinfo结构信息
定义loader区的信息
3) mstsave结构信息
定义核心线程的状态信息,错误线程的mstsave结构信息直接存储在core文件的头部区,此区域只对多线程的程序有效,除错误线程外的其他线程的mstsave结构信息存与此区域。
4) 缺省的用户堆栈数据
存储coredump时的用户堆栈数据
5) 缺省的数据区域
存储用户数据区域信息
6) 内存映射数据
存储匿名的内存映射数据
7) vm_info结构信息
存储内存映射区域的地址偏移量和大小信息。缺省情况下,用户数据、匿名的内存区域和vm_info结构信息并不包含在core文件中,core文件值包含当前的进程堆栈、线程堆栈、线程mstsave结构、用户结构和错误时的寄存器信息,这些信息足够跟踪错误的产生。
8.core文件分析
首先分析coredump的结构组成,core文件的头信息是由结构core_dump结构定义的,结构成员定义如下:
成员 类型 描述
c_signo char 引起错误的信号量
C_entries ushort Coredump的模块数
*c_tab Struct ld_info Core数据的地址偏移量
c_flag char 描述coredump的类型,类型为:
FULL_CORECore包含数据区域
CORE_VERSION_1生成 core文件的AIX的版本
MSTS_VALID包含mstsave的结构
CORE_BIGDATACore文件包含大数据
UBLOCK_VALIDCore文件包含u_block结构
USTACK_VALIDCore文件包含用户堆栈数据
LE_VALIDCore文件至少包含一个模块
CORE_TRUNCCore文件被截短
c_stack Caddr_t 用户堆栈的起始地址偏移量
C_size int 用户堆栈的大小
C_mst Struct mstsave 错误mst的拷贝
C_u Struct user 用户结构的拷贝
C_nmsts int Mstsave结构的数量
C_msts Struct mstsvae * 线程的mstsave结构的地址偏移量
C_datasize int 数据区域的大小
C_data Caddr_t 用户数据的地址偏移量
C_vmregions int 匿名地址映射的数量
C_vmm Struct vm_info * Vm_info数据表的起始地址偏移量
借助于下面提供的程序可以分析core文件的部分信息:(程序未经验证)
a.分析程序any.c???Q4:==>>测试程序
#include <stdio.h> #include <sys/core.h> int main(int argc, char *argv[]) { FILE *corefile; struct core_dumpx c_file; char command[256]; if (argc != 2) { fprintf(stderr, "Usage: %s <corefile>\n", *argv); exit(1); } if ((corefile = fopen(argv[1], "r")) == NULL) { perror(argv[1]); exit(1); } fread(&c_file, sizeof(c_file), 1, corefile); fclose(corefile); sprintf(command, "lquerypv -h %s 6E0 64 | head -1 | awk '{print $6}'", argv[1]); printf("Core created by: \n"); system(command); printf("Signal number and cause of error number: %i\n", c_file.c_signo); printf("Core file type: %i\n", c_file.c_flag); printf("Number of core dump modules: %i\n", c_file.c_entries); printf("Core file format number: %i\n", c_file.c_version); printf("Thread identifier: %i\n", c_file.c_flt.th.ti_tid); printf("Process identifier: %i\n", c_file.c_flt.th.ti_pid); printf("Current effective priority: %i\n", c_file.c_flt.th.ti_pri); printf("Processor Usage: %i\n", c_file.c_flt.th.ti_cpu); printf("Processor bound to: cpu%i\n", c_file.c_flt.th.ti_cpuid); /* if (c_file.c_flt.th.ti_cpu > 1) printf("Last Processor: cpu%i\n", c_file.c_flt.th.ti_affinity); */ return 0; }
gcc -o any any.c
b.产生core file的程序core.cint main() { char *testadd; strcpy(testadd, "Just a testing"); }gcc -o core_test core.c