如何产生core dump文件

发布时间 2023-10-18 15:16:47作者: 半山随笔

首先是系统设置:

ulimit -c unlimited

  将core文件的大小限制设置成无限制,当然也可以指定大小。

sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t

  设置core文件的位置及格式。

其次在编译的时候打开调试模式:

gcc -ggdb -o0 file.c -o bin

  运行程序,如果发生core dump,将会在/tmp下产生以core开头的文件,就是core dump文件。

举个例子:

// file.c
int main(void)
{
    char *p;
    *p = 'a';

    return 0;
}

 编译:

gcc -ggdb -o0 file.c -o bin

 运行

./bin
Segmentation fault (core dumped)

 在/tmp下生成一个core文件:core-main.206270.xxx.1697612689

调试core文件

gdb bin /tmp/core-bin.206270.xxx.1697612689

 得到:

Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000aaad43140720 in main () at main.c:4
4           *p = 'a';
(gdb)