标签归档:core

coredump文件生成样例测试源码,修改coredump大小及目录

 用c编写的程序在运行出错时,系统会生成coredump文件,如果系统没有生成可以通过命令

ulimit -c unlimited

1:添加pid作为扩展名,生成的core文件名称为core.pid
0:不添加pid作为扩展名,生成的core文件名称为core
修改 /proc/sys/kernel/core_uses_pid 文件内容为: 1
修改文件命令: echo “1” > /proc/sys/kernel/core_uses_pid
或者
sysctl -w kernel.core_uses_pid=1 kernel.core_uses_pid = 1

b. 控制core文件保存位置和文件名格式
修改文件命令: echo “/corefile/core-%e-%p-%t” > /proc/sys/kernel/core_pattern
或者:
sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t kernel.core_pattern = /corefile/core-%e-%p-%t
可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
%p – insert pid into filename 添加pid(进程id)
%u – insert current uid into filename 添加当前uid(用户id)
%g – insert current gid into filename 添加当前gid(用户组id)
%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 添加导致产生core的命令名
——————— 
作者:faithfu_yy 
来源:CSDN 
原文:https://blog.csdn.net/u011417820/article/details/71435031 
版权声明:本文为博主原创文章,转载请附上博文链接!

1)coredump文件生成样例测试源码

使系统生成产生core文件,这样就可以利用core文件查看程序是在哪一行出现错误了,具体的方法如下:
1、程序编译时要加-g选项,保证debug信息生成在应用程序当中

2、如果运行过程中出错,执行下面命令查看程序哪里出现错误:

gdb a.out core

举例来说:
#include <stdio.h>
int main(int argc, char** argv) {
  int* p = NULL;
  *p = 10;
}

上面的程序会运行出错,用g++ -g编译后,生成a.out,运行a.out,产生core文件,当执行gdb a.out core时,gdb会自动停止在出错的位置。