如何在我的 Linux C++ 程序中启用核心转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2919378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable core dump in my Linux C++ program
提问by Alex F
My program is written in C++. compiled with gcc, using -g3 -O0 -ggdb flags. When it crashes, I want to open its core dump. Does it create core dump file, or I need to do something to enable core dump creation, in the program itself, or on computer where it is executed? Where this file is created, and what is its name?
我的程序是用 C++ 编写的。使用 gcc 编译,使用 -g3 -O0 -ggdb 标志。当它崩溃时,我想打开它的核心转储。它会创建核心转储文件,还是我需要在程序本身或执行它的计算机上做一些事情来启用核心转储创建?这个文件是在哪里创建的,它的名字是什么?
采纳答案by Alex F
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>
.
您需要设置ulimit -c
. 如果此参数为 0,则不会创建核心转储文件。所以这样做:ulimit -c unlimited
并检查一切是否正确ulimit -a
。当应用程序做了一些不适当的事情时,就会创建核心转储文件。我系统上的文件名是core.<process-pid-here>
.
回答by msw
By default many profiles are defaulted to 0 core file size because the average user doesn't know what to do with them.
默认情况下,许多配置文件默认为 0 核心文件大小,因为普通用户不知道如何处理它们。
Try ulimit -c unlimited
before running your program.
ulimit -c unlimited
在运行程序之前尝试。
回答by user2167243
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);