如何在我的 Linux C + + 程序中启用核心转储

我的程序是用 C + + 写的。使用-g3-O0-ggdb 标志用 gcc 编译。当它崩溃时,我想打开它的核心转储。它是创建核心转储文件,还是需要在程序本身或执行它的计算机上启用核心转储创建?这个文件是在哪里创建的,它的名称是什么?

116408 次浏览

You need to set ulimit -c. If you have 0 for this parameter a coredump file is not created. So do this: ulimit -c unlimited and check if everything is correct ulimit -a. The coredump file is created when an application has done for example something inappropriate. The name of the file on my system is core.<process-pid-here>.

By default many profiles are defaulted to 0 core file size because the average user doesn't know what to do with them.

Try ulimit -c unlimited before running your program.

You can do it this way inside a program:

#include <sys/resource.h>


// core dumps may be disallowed by parent of this process; change that
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);