Coredump

业务进程宕机以后开发需要查看dump查找定位问题,所有服务器初始化环境时候统一定制coredump路径;

1.Core文件简介

Core文件其实就是内存的映像,当程序崩溃时,存储内存的相应信息,主用用于对程序进行调试。当程序崩溃时便会产生core文件,其实准确的应该说是core dump 文件,默认生成位置与可执行程序位于同一目录下,文件名为core.***,其中***是某一数字。

2.开启或关闭Core文件的生成

关闭或阻止core文件生成:

$ulimit -c 0

打开core文件生成:

$ulimit -c unlimited

检查core文件的选项是否打开:

$ulimit -a

ulimit参数含义如下: 

             -a     All current limits are reported
              -c     The maximum size of core files created
              -d     The maximum size of a process data segment
              -e     The maximum scheduling priority ("nice")
              -f     The maximum size of files written by the shell and its children
              -i     The maximum number of pending signals
              -l     The maximum size that may be locked into memory
              -m     The maximum resident set size (has no effect on Linux)
              -n     The maximum number of open file descriptors (most systems do not allow this value to be set)
              -p     The pipe size in 512-byte blocks (this may not be set)
              -q     The maximum number of bytes in POSIX message queues
              -r     The maximum real-time scheduling priority
              -s     The maximum stack size
              -t     The maximum amount of cpu time in seconds
              -u     The maximum number of processes available to a single user
              -v     The maximum amount of virtual memory available to the shell
              -x     The maximum number of file locks

 

#ulimit -a

core file size          (blocks, -c) unlimited

data seg size           (kbytes, -d) unlimited

scheduling priority             (-e) 0

file size               (blocks, -f) unlimited

pending signals                 (-i) 127974

max locked memory       (kbytes, -l) 64

max memory size         (kbytes, -m) unlimited

open files                      (-n) 100000

pipe size            (512 bytes, -p) 8

POSIX message queues     (bytes, -q) 819200

real-time priority              (-r) 0

stack size              (kbytes, -s) 8192

cpu time               (seconds, -t) unlimited

max user processes              (-u) 655360

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited而我们需要修改的是open files (-n) 1024的值

于是命令就是limit -n 2048(随各自需要设置)

 

 

 

以上配置只对当前会话起作用,下次重新登陆后,还是得重新配置。要想配置永久生效,得在/etc/profile或者/etc/security/limits.conf文件中进行配置。

首先以root权限登陆,然后打开/etc/security/limits.conf文件,进行配置:

#vim /etc/security/limits.conf

<domain>    <type>    <item>        <value>

       *              soft          core         unlimited

或者在/etc/profile中作如下配置:

#vim /etc/profile

ulimit -S -c unlimited >/dev/null 2>&1

或者想配置只针对某一用户有效,则修改此用户的~/.bashrc或者~/.bash_profile文件:

limit -c unlimited

ulimit -c 0 是禁止产生core文件,而ulimit -c 1024则限制产生的core文件的大小不能超过1024kb

3.设置Core Dump的核心转储文件目录和命名规则

/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0
/proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e
可以这样修改:
echo "/data/common/coredump/core-%e-%p-%t" >/proc/sys/kernel/core_pattern
将会控制所产生的core文件会存放到/data/common/coredump目录下,产生的文件名为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 添加命令名

4.core文件的使用

core文件所在目录下键入:
gdb -c core   -c指定core文件)
它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等
如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成core.12345,那么用此指令调试:
gdb -c core MyServer

 源:https://www.cnblogs.com/xiaodoujiaohome/p/6222895.html 

5.永久生效:
当前不重启生效:
你可以用下列方式来完成
sysctl -w kernel.core_pattern=/data/common/coredump/core-%e-%p-%t 

echo "/data/common/coredump/core-%e-%p-%t" >/proc/sys/kernel/core_pattern

这些操作一旦计算机重启,则会丢失,如果你想持久化这些操作,可以在 /etc/sysctl.conf文件中增加:
kernel.core_pattern=/data/common/coredump/core-%e-%p-%t 

加好后,如果你想不重启看看效果的话,则用下面的命令:
sysctl -p /etc/sysctl.conf

# sysctl -p /etc/sysctl.conf

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

net.ipv6.conf.lo.disable_ipv6 = 1

net.ipv4.conf.all.promote_secondaries = 1

net.ipv4.conf.default.promote_secondaries = 1

net.ipv6.neigh.default.gc_thresh3 = 4096

net.ipv4.neigh.default.gc_thresh3 = 4096

kernel.softlockup_panic = 1

kernel.sysrq = 1

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

vm.overcommit_memory = 1

kernel.numa_balancing = 0

kernel.shmmax = 68719476736

kernel.printk = 5

kernel.core_pattern = /data/common/coredump/core-%e-%p-%t