菜鸟笔记
提升您的技术认知

linux下core file size设置笔记

现象说明:突然发现一台测试机器的java程序莫名其妙地没了,但是没有core dump!这就需要打开服务器的core文件生成的功能了,(即core dump文件),方便程序调试。
1)core文件简介
core文件其实就是内存的映像,当程序崩溃时,存储内存的相应信息,主用用于对程序进行调试。当程序崩溃时便会产生core文件,其实准确的应该说是core dump 文件,默认生成位置与可执行程序位于同一目录下,文件名为core.***,其中***是某一数字。
2)开启或关闭core文件的生成

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 查看core文件生成功能是否打开,如下若是0,则表示没有打开。 [root@localhost ~] # ulimit -c 0   临时设置(如下设置2G,单位为kbyte) 如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文 件的时候, gdb 会提示错误。 [root@localhost ~] # ulimit -c 4194304   永久设置 [root@localhost ~] # echo " * soft core 4194304" >> /etc/security/limits.conf [root@localhost ~] # echo " * hard core 4194304" >> /etc/security/limits.conf   [root@localhost ~] # ulimit -a core  file  size          (blocks, -c) 4194304 data seg size           (kbytes, -d) unlimited scheduling priority             (-e) 0 file  size               (blocks, -f) unlimited pending signals                 (-i) 15189 max locked memory       (kbytes, -l) 64 max memory size         (kbytes, -m) unlimited open  files                      (-n) 65535 pipe size            (512 bytes, -p) 8 POSIX message queues     (bytes, -q) 819200 real- time  priority              (-r) 0 stack size              (kbytes, -s) 10240 cpu  time                (seconds, -t) unlimited max user processes              (-u) 102400 virtual memory          (kbytes, - v ) unlimited file  locks                      (-x) unlimited   ulimit  -c unlimited,则表示core文件的大小不受限制 [root@localhost ~] # ulimit -c unlimited

3)core文件的使用

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

4)产生core文件的测试

1 2 3 4 5 6 7 8 9 10 11 直接输入指令: [root@localhost ~] # kill -s SIGSEGV $$   扩展: ulimint -a 用来显示当前的各种用户进程限制   Linux对于每个用户,系统限制其最大进程数,为提高性能,可以根据设备资源情况, 设置个Linux用户的最大进程数,一些需要设置为无限制: 数据段长度: ulimit  -d unlimited 最大内存大小: ulimit  -m unlimited 堆栈大小: ulimit  -s unlimited

5)core文件的名称和生成路径

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 core文件生成路径: 输入可执行文件运行命令的同一路径下。   若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。   1) /proc/sys/kernel/core_uses_pid 可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名, 生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。 可通过以下命令修改此文件: # echo "1" > /proc/sys/kernel/core_uses_pid   2)proc /sys/kernel/core_pattern 可以控制core文件保存位置和文件名格式。 可通过以下命令修改此文件,可以将core文件统一生成到 /corefile 目录下,产生的文件名为core-命令名-pid-时间戳 # echo "/corefile/core-%e-%p-%t" > core_pattern   以下是参数列表:      %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 添加命令名