软硬件开发技术笔记
保持专注,拒绝内耗
linux环境主动触发进程产生coredump
2022-03-21 20:51

程序运行过程中发现有内存泄漏,需要主动触发coredump用于分析,可以按以下步骤操作

配置abrt

# 安装abrt包
[root@home-dev8 ~]# yum install -y abrt abrt-addon-ccpp

# 编辑配置文件
[root@home-dev8 ~]# vim /etc/abrt/abrt.conf
...
MaxCrashReportsSize = 0     # 修改配置文件中该项值为0
...

# 编辑配置文件
[root@home-dev8 ~]# vim /etc/abrt/abrt-action-save-package-data.conf
...
OpenGPGCheck = no           # 修改该项
ProcessUnpackaged = yes     # 修改该项
...

# 启动服务,配置好后,可以不重启操作系统生效
[root@home-dev8 ~]# systemctl restart abrtd
[root@home-dev8 ~]# systemctl restart abrt-ccpp  

# 设置开机启动启动
[root@home-dev8 ~]# systemctl enable abrtd
[root@home-dev8 ~]# systemctl enable abrt-ccpp

测试产生coredump

# 创建以下c程序,用于产生测试coredump
[root@home-dev8 ~]# cat test1.c
int main() {char *c; int a=*c;}

# 编译
[root@home-dev8 ~]# yum install gcc -y 
[root@home-dev8 ~]# gcc -g test1.c -o test1

# 执行,执行后,如果可以在/var/spool/abrt下看到生成ccpp-xx目录,说明配置成功
[root@home-dev8 ~]# ./test1 
Segmentation fault (core dumped)

# 查看coredump文件
[root@home-dev8 ~]# ll /var/spool/abrt
total 8
drwxr-x---. 2 root abrt 4096 Mar 21 12:53 ccpp-2022-03-21-12:53:17-18136
-rw-------. 1 root root   11 Mar 21 12:53 last-ccpp

主动触发coredump

本质是通过kill命令发送信号触发coredump,可以触发的信号有很多,常用的的信号SIGABRTSIGSEGV

# 编写测试程序
[root@home-dev8 ~]# cat test2.c
int main() {sleep(1000);}

# 编译
[root@home-dev8 ~]# gcc -g test2.c -o test2

# 执行,此时程序会一直阻塞在这
[root@home-dev8 ~]# ./test2 

# 另外开一个终端,查看进程pid
[root@home-dev8 ~]# ps -elf | grep test2
0 S root     18298 17885  0  80   0 -  1052 hrtime 13:16 pts/0    00:00:00 ./test2
0 S root     18300 18227  0  80   0 - 28177 -      13:16 pts/1    00:00:00 grep --color=auto test2

# 通过kill发送SIGABRT信号,或者kill -11发送SIGSEGV信号,可通过kill -l获取-6/-11及其他信号名称的对应ID
[root@home-dev8 ~]# kill -6 18298

# 查看coredump
[root@home-dev8 ~]# ll /var/spool/abrt
total 8
drwxr-x---. 2 root abrt 4096 Mar 21 13:16 ccpp-2022-03-21-13:16:13-18298
-rw-------. 1 root root   11 Mar 21 13:16 last-ccpp

# gdb调试
[root@home-dev8 ~]# yum install gdb -y
[root@home-dev8 ~]# gdb /root/test2 /var/spool/abrt/ccpp-2022-03-21-13\:16\:13-18298/coredump 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/test2...done.
[New LWP 18298]
Core was generated by `./test2'.
Program terminated with signal 6, Aborted.
#0  0x00007f3da6875e10 in __nanosleep_nocancel () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64
(gdb) bt
#0  0x00007f3da6875e10 in __nanosleep_nocancel () from /lib64/libc.so.6
#1  0x00007f3da6875cc4 in sleep () from /lib64/libc.so.6
#2  0x0000000000400530 in main () at test2.c:1
(gdb) l
1   int main() {sleep(1000);}
(gdb) 
分类
mac
1篇
4篇
c
1篇
4篇
2篇
1篇
搜索