C语言之gdb排查崩溃之core文件

一、编译程序(-g -rdynamic)

编译源代码时指定-g -rdynamic参数,编译带有调试信息的可执行程序

二、打开core dump限制

1、通过指令打开

使用ulimit -a指令来查看系统相关的限制参数。

[11:05@zhouchao ~/c_c++]$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) 30773
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) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

==注意==:通过ulimit -c unlimitedulimit -c 1024ulimit -c 0等指令来配置core dump功能,其中unlimited表示不限制,1024表示最多1024个块,0表示关闭该功能。

2、修改配置文件

修改配置文件/etc/security/limits.conf,找到#* soft core 0修改为#* soft core unlimited即可。
注意:该配置文件是一个只读文件,需要root权限chmod后才能修改内容。

 50 #                 
 51 #
 52 
 53 #*               soft    core            0
 54 #*               hard    rss             10000
 55 #@student        hard    nproc           20
 56 #@faculty        soft    nproc           20
 57 #@faculty        hard    nproc           50
 58 #ftp             hard    nproc           0

三、core文件名称和路径

默认生成路径:可执行文件运行命令的同一路径。
默认生成名称:core,新的文件会覆盖就的core文件。

1、设置pid作为文件扩展名

root权限下修改/proc/sys/kernel/core_uses_pid文件内容为1或0.
sysctl -w kernel.core_uses_pid=1
echo "0" > /proc/sys/kernel/core_uses_pid

1:添加pid作为文件扩展名,生成core文件为core.pid.
0:不添加pid作为文件扩展名,生成core文件为core.

[11:19@root ~]$cat /proc/sys/kernel/core_uses_pid       
0
[11:20@root ~]$sysctl -w kernel.core_uses_pid=1
kernel.core_uses_pid = 1
[11:20@root ~]$cat /proc/sys/kernel/core_uses_pid
1
[11:20@root ~]$echo "0" > /proc/sys/kernel/core_uses_pid
[11:20@root ~]$cat /proc/sys/kernel/core_uses_pid       
0
[11:20@root ~]$

2、控制core文件的保存为之和文件名格式

root权限下修改/proc/sys/kernel/core_pattern文件内容.
echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
sysctl -w kernel.core_pattern=/corefile/core.%e.%p.%s.%E

%p:进程ID(4206)
%u:用户ID(1002)
%g:用户组ID(1000)
%s:添加导致产生core的信号(11)
%t:添加core文件生成时的Unix时间(1640230181)
%h:添加主机名(localhost.localdomain)
%e:添加导致产生core的命令名(seg.bin)

[11:28@root ~]$cat /proc/sys/kernel/core_pattern
core.%e.%p.%u.%s.%g.%h.%t.%E
[11:28@root ~]$sysctl -w kernel.core_pattern=core.%e.%p.%u.%s.%g.%h.%t
kernel.core_pattern = core.%e.%p.%u.%s.%g.%h.%t
[11:33@root ~]$echo "core.%e.%p.%u.%s.%g.%h.%t" > /proc/sys/kernel/core_pattern
[11:34@root ~]$cat /proc/sys/kernel/core_pattern
core.%e.%p.%u.%s.%g.%h.%t
[11:34@root ~]$ls
core.seg.bin.13042.1002.11.1000.localhost.localdomain.1640230507

3、测试是否生效

执行kill -s SIGSEGV $$指令,查看当前目录下是否生成了core文件。

[11:34@root ~]$kill -s SIGSEGV $$
Segmentation fault (core dumped)
[11:37@zhouchao ~/c_c++]$ls
core.bash.2697.1002.11.1000.localhost.localdomain.1640230610
[11:39@zhouchao ~/c_c++]$gdb -q bash core.bash.2697.1002.11.1000.localhost.localdomain.1640230610
Reading symbols from bash...
Reading symbols from .gnu_debugdata for /usr/bin/bash...
(No debugging symbols found in .gnu_debugdata for /usr/bin/bash)
[New LWP 2697]
Core was generated by `-bash'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f5d0d8df6a7 in kill () from /usr/lib64/libc.so.6
(gdb) bt
#0  0x00007f5d0d8df6a7 in kill () from /usr/lib64/libc.so.6
#1  0x0000000000433bab in execute_command ()
#2  0x000000000041e285 in reader_loop ()
#3  0x000000000041c8ee in main ()
(gdb) _

四、通过core文件排查错误

执行gdb [exec-file] [core-file]gdb -c [core-file] [exec-file]指令来查看core文件内容,通过bt指令查看堆栈信息。

[11:45@zhouchao ~/c_c++]$gdb -q ./seg core.seg.27060.11.1640231023 
Reading symbols from ./seg...
[New LWP 27060]
Core was generated by `./seg'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400885 in test_ptr (ptr=0x0) at test.c:21
21          printf("ptr = %p, a = %u, b = %f.\n", ptr, ptr->a, (*ptr).b);
(gdb) bt
#0  0x0000000000400885 in test_ptr (ptr=0x0) at test.c:21
#1  0x0000000000400912 in main () at test.c:33
(gdb) q
[11:46@zhouchao ~/c_c++]$gdb -q -c core.seg.27060.11.1640231023 ./seg 
Reading symbols from ./seg...
[New LWP 27060]
Core was generated by `./seg'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400885 in test_ptr (ptr=0x0) at test.c:21
21          printf("ptr = %p, a = %u, b = %f.\n", ptr, ptr->a, (*ptr).b);
(gdb) bt
#0  0x0000000000400885 in test_ptr (ptr=0x0) at test.c:21
#1  0x0000000000400912 in main () at test.c:33
(gdb) q
[11:46@zhouchao ~/c_c++]$

你可能感兴趣的:(C语言之gdb排查崩溃之core文件)