1. 啟用 Core dump,系統(tǒng)默認(rèn)關(guān)閉
Core文件其實(shí)就是內(nèi)存的映像,當(dāng)程序崩潰時(shí),存儲(chǔ)內(nèi)存的相應(yīng)信息,主用用于對(duì)程序進(jìn)行調(diào)試。
用到的命令是: ulimit
// 輸入目錄查看狀態(tài), 0 說(shuō)明是禁止生成 core 文件。
[root@ ~]# ulimit -c
0
我們可以直接在 ulimit -c 后面加上參數(shù),當(dāng)為數(shù)字時(shí)表示限制 core 文件大小不超過(guò) 1024KB[ulimit -c 1024]:
# unlimited: 字面意思,沒(méi)有限制 core 文件大小。
[root@ ~]# ulimit -c unlimited
# 不是 root 用戶可能會(huì)失敗,如 Ubuntu 啟用了 root,但不能用 sudo 去執(zhí)行,而要 su root 切換過(guò)去才能成功執(zhí)行
[非root用戶@ ~]$ ulimit -c unlimited
bash: ulimit: core file size: 無(wú)法修改 limit 值: 不允許的操作
2. 設(shè)置 core 文件的存儲(chǔ)目錄和命名格式
設(shè)置 core 的存儲(chǔ)目錄和命名格式,主要是修改配置文件 /proc/sys/kernel/core_pattern:
# 1. 默認(rèn)在當(dāng)前程序執(zhí)行目錄下生成,core-程序名-程序pid-時(shí)間 [core-test-3451-1516257740]
[root@ ~]# echo "core-%e-%p-%t" > /proc/sys/kernel/core_pattern
# 2. 添加路徑,可以把所有的 core 集中到一個(gè)文件夾里 [把所有的core文件放到 /root/core-file 目錄下]
[root@ ~]# echo "/root/core-file/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
3. core dump 用法
1、首先,創(chuàng)建編寫(xiě)一個(gè)簡(jiǎn)單的小程序,將內(nèi)存釋放兩次:
// FILE:test.c
#include<stdlib.h>
void repeatFree(char *p)
{
if(NULL != p)
{
free(p);
}
}
int main()
{
char* pstr =(char*) malloc(10);
repeatFree(pstr); // 第一次釋放
repeatFree(pstr); // 第二次釋放
return 0;
}
2、然后,gcc 編譯,加 -g 再調(diào)試是可以看得更詳細(xì):
# 編譯
[root@ ~]# gcc -g test.c -o test
# 運(yùn)行
[root@ ~]# ./test
*** Error in `./test': double free or corruption (top): 0x0000000001078010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f753c2e47e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f753c2ed37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f753c2f153c]
./test[0x400585]
./test[0x4005b6]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f753c28d830]
./test[0x400499]
........一堆的內(nèi)存問(wèn)題........
# 可以看到,當(dāng)前目錄下生成了一個(gè) core 文件(如果你配置在當(dāng)前目錄生成 core 文件的話)
[root@ ~]# ls
core-test-19317-1516269504 test test.c
3、gdb 調(diào)試,找出出錯(cuò)的位置 gdb 程序名 core文件名:
[root@ ~]# gdb test core-test-19317-1516269504
[New LWP 19317]
.......一些基本信息.......
Core was generated by `./test'.
Program terminated with signal SIGABRT, Aborted.
.......一些錯(cuò)誤信息.......
(gdb) where
#0 0x00007f753c2a2428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007f753c2a402a in __GI_abort () at abort.c:89
#2 0x00007f753c2e47ea in __libc_message (do_abort=do_abort@entry=2,
fmt=fmt@entry=0x7f753c3fde98 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007f753c2ed37a in malloc_printerr (ar_ptr=<optimized out>, ptr=<optimized out>,
str=0x7f753c3fdf88 "double free or corruption (top)", action=3) at malloc.c:5006
#4 _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3867
#5 0x00007f753c2f153c in __GI___libc_free (mem=<optimized out>) at malloc.c:2968
#6 0x0000000000400585 in repeatFree (p=0x1078010 "") at test.c:8
#7 0x00000000004005b6 in main () at test.c:18
(gdb)
在 gdb 內(nèi),輸入 where 可以看出, 我們寫(xiě)的程序出錯(cuò)的兩行:
#6 0x0000000000400585 in repeatFree (p=0x1078010 "") at test.c:8
#7 0x00000000004005b6 in main () at test.c:18
在 repeatFree 函數(shù)中,test.c 文件的第 8 行,也就是下面這行錯(cuò)啦,釋放了兩次內(nèi)存:
8 free(p);
其他的具體應(yīng)用還沒(méi)涉及,后面再遇到類(lèi)似的問(wèn)題,再深入研究。
[reference]
[1] tfjay914. Linux coredump解決流程[M]. (2017-03-10 10:26:19) http://blog.51cto.com/terrytong914/1905041
[2] mrjy1475726263. linux下生成core dump文件方法及設(shè)置[M]. ( 2015年03月07日 14:27:51) http://blog.csdn.net/mrjy1475726263/article/details/44116289/