关闭系统天生 core 文件 :ulimit -c 0
检讨天生 core 文件的选项是否打开 :ulimit -a该命令将显示所有的用户定制,个中选项 -a 代表“ all ”。
系统文件调度 core 选项:/etc/profile # No core files by defaultulimit -S -c 0 > /dev/null 2>&1

用户自定义调度 core 选项:在用户的 ~/.bash_profile 里加上 ulimit -c unlimited 来让特定的用户可以产生 core 文件。
在终端中输入ulimit -c 如果结果为0,解释当程序崩溃时,系统并不能天生core dump。
利用ulimit -c unlimited命令,开启core dump功能,并且不限定天生core dump文件的大小。如果须要限定,加数字限定即可。ulimit - c 1024
默认情形下,core dump天生的文件名为core,而且就在程序当前目录下。新的core会覆盖已存在的core。通过修正/proc/sys/kernel/core_uses_pid文件,可以将进程的pid作为作为扩展名,天生的core文件格式为core.xxx,个中xxx即为pid
设置 Core Dump 的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid 可以掌握产生的 core 文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0proc/sys/kernel/core_pattern 可以设置格式化的 core 文件保存位置或文件名 ,比如原来文件内容是 core-%e可以这样修正 :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 添加命令名
测试产生 core 文件直接输入指令 : kill -s SIGSEGV $$
如何产生Core Dump
发生doredump一样平常都是在进程收到某个旗子暗记的时候,Linux上现在大概有60多个旗子暗记,可以利用 kill -l 命令全部列出来。
sagi@sagi-laptop:~$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
针对特定的旗子暗记,运用程序可以写对应的旗子暗记处理函数。如果不指定,则采纳默认的处理办法, 默认处理是coredump的旗子暗记如下:
3)SIGQUIT 4)SIGILL 6)SIGABRT 8)SIGFPE 11)SIGSEGV 7)SIGBUS 31)SIGSYS5)SIGTRAP 24)SIGXCPU 25)SIGXFSZ 29)SIGIOT
我们看到SIGSEGV在个中,一样平常数组越界或是访问空指针都会产生这个旗子暗记。其余虽然默认是这样的,但是你也可以写自己的旗子暗记处理函数改变默认行为,更多旗子暗记干系可以看参考链接33。
上述内容只是产生coredump的必要条件,而非充分条件。要产生core文件还依赖于程序运行的shell,可以通过ulimit -a命令查看,输出内容大致如下:
sagi@sagi-laptop:~$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 20 file size (blocks, -f) unlimited pending signals (-i) 16382 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 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) unlimited virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
core file size,这个值用来限定产生的core文件大小,超过这个值就不会保存了