0
点赞
收藏
分享

微信扫一扫

linux 下生成 core dump 配置和用法

花海书香 2023-05-31 阅读 32

1. 启用 Core dump,系统默认关闭

Core文件其实就是内存的映像,当程序崩溃时,存储内存的相应信息,主用用于对程序进行调试。

用到的命令是: ulimit


// 输入目录查看状态, 0 说明是禁止生成 core 文件。
[root@ ~]# ulimit -c
0

我们可以直接在 ulimit -c 后面加上参数,当为数字时表示限制 core 文件大小不超过 1024KB[ulimit -c 1024]:


# unlimited: 字面意思,没有限制 core 文件大小。
[root@ ~]# ulimit -c unlimited
# 不是 root 用户可能会失败,如 Ubuntu 启用了 root,但不能用 sudo 去执行,而要 su root 切换过去才能成功执行
[非root用户@ ~]$ ulimit -c unlimited
bash: ulimit: core file size: 无法修改 limit 值: 不允许的操作

2. 设置 core 文件的存储目录和命名格式

设置 core 的存储目录和命名格式,主要是修改配置文件 /proc/sys/kernel/core_pattern


# 1. 默认在当前程序执行目录下生成,core-程序名-程序pid-时间 [core-test-3451-1516257740]
[root@ ~]# echo "core-%e-%p-%t" > /proc/sys/kernel/core_pattern
# 2. 添加路径,可以把所有的 core 集中到一个文件夹里 [把所有的core文件放到 /root/core-file 目录下]
[root@ ~]# echo "/root/core-file/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

3. core dump 用法

1、首先,创建编写一个简单的小程序,将内存释放两次:

// 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 再调试是可以看得更详细:


# 编译
[root@ ~]# gcc -g test.c -o test
# 运行
[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]
........一堆的内存问题........

# 可以看到,当前目录下生成了一个 core 文件(如果你配置在当前目录生成 core 文件的话)
[root@ ~]# ls
core-test-19317-1516269504  test  test.c

3、gdb 调试,找出出错的位置 gdb 程序名 core文件名

[root@ ~]# gdb test core-test-19317-1516269504
[New LWP 19317]
.......一些基本信息.......
Core was generated by `./test'.
Program terminated with signal SIGABRT, Aborted.
.......一些错误信息.......
(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 内,输入 where 可以看出, 我们写的程序出错的两行:

#6  0x0000000000400585 in repeatFree (p=0x1078010 "") at test.c:8
#7  0x00000000004005b6 in main () at test.c:18

在 repeatFree 函数中,test.c 文件的第 8 行,也就是下面这行错啦,释放了两次内存:

8       free(p);

举报

相关推荐

0 条评论