linux设置启用生成core文件

某些时候,需要监测程序异常退出的问题,可以使用生成core文件方式来定位问题

使用以下命令查看是否启用了core文件生成

ulimit -a


结果如下所示

linux-hhy2:~ # ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
pending signals                 (-i) 40960
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 40960
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

在第一行中显示core file size (blocks, -c) 这个值为0则无法生成core文件,可以使用以下命令来设置启用core文件生成

ulimit -c 1024

或者

ulimit -c unlimited

再次命令

ulimit -a

可以看到,显示已经启用core文件生成

linux-hhy2:~ # ulimit -c unlimited
You have new mail in /var/mail/root
linux-hhy2:~ # ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
pending signals                 (-i) 40960
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 40960
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

这样,当程序异常退出时,会在程序目录中生成core.xxxx的文件,然后使用输入gdb –c core文件 –f 执行文件来查看core内容

gdb -c core.22960 -f /opt/webserver/bin/server

然后输入bt查看调用堆栈

另一种生成core文件的方式,是使用gcore命令,首先使用ps -ef | grep my命令查看进程号,然后使用gcore -o 文件名 进程ID命令来产生core文件

linux-hhy2:~ # ps -ef | grep my
linux-hhy2:~ # gcore -o /opt/webserver/bin/testcore 4969

Comments are closed.