linux 生成的core文件名被截断问题分析

生成core文件的指定生成目录和文件名:

修改/etc/sysctl.conf添加一行:

kernel.core_pattern = /tmp/core-%e-%u-%s-%t-%h-%p

/sbin/sysctl –p 立即生效

则生成的core文件在/tmp目录,参数解释:

%e - insert coredumping executable name into filename 添加命令名
%u - insert current uid into filename 添加当前uid
%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 添加主机名
%p - insert pid into filename 添加pid

问题出在%e,导致core的程序名,如果名字很长的话,core文件中这一项会被截断为15个字符长度,通过查找资料:

The code for this can be found in exec.c here.

The code is going to copy the corename based on the pattern up to the first percentage (giving /cores/core.). At the percentage it's going to increment and process the 'e'. The code for processing the 'e' part prints out the pattern using snprintf based on the current->comm structure.

This is the executable name (excluding path) TRUNCATED to the value TASK_COMM_LEN. Since this is defined as 16 characters (at least in the Kernel I found) then SampleCrashApplication is truncated to 15 + 1 characters (1 for the null byte at the end) which explains why you get your truncated core dump name.

所以内核汇总有个宏 TASK_COMM_LEN限制了长度,经过验证:

vim include/linux/sched.h +162


果然有这个限制,所以在程序命名的时候尽量短一点,或者修改内核重新编译安装内核。

你可能感兴趣的:(linux内核,linux编程)