Linux 如何以编程方式导致 C/C++ 中的核心转储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/979141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:24:32  来源:igfitidea点击:

How to programmatically cause a core dump in C/C++

c++clinuxcoredumpabort

提问by hhafez

I would like to force a core dump at a specific location in my C++ application.

我想在我的 C++ 应用程序中的特定位置强制进行核心转储。

I know I can do it by doing something like:

我知道我可以通过执行以下操作来做到这一点:

int * crash = NULL;
*crash = 1;

But I would like to know if there is a cleaner way?

但我想知道是否有更清洁的方法?

I am using Linux by the way.

顺便说一下,我正在使用 Linux。

采纳答案by paxdiablo

Raising of signal number 6 (SIGABRTin Linux) is one way to do it (though keep in mind that SIGABRT is not requiredto be 6 in all POSIX implementations so you may want to use the SIGABRTvalue itself if this is anything other than quick'n'dirty debug code).

提高信号编号 6(SIGABRT在 Linux 中)是一种方法(但请记住,SIGABRT在所有 POSIX 实现中不需要为 6,因此SIGABRT如果这不是 quick'n '脏调试代码)。

#include <signal.h>
: : :
raise (SIGABRT);

Calling abort()will also cause a core dump, and you can even do this withoutterminating your process by calling fork()followed by abort()in the child only - see this answerfor details.

调用abort()也会导致核心转储,您甚至可以在终止进程的情况下执行此操作方法是仅在子进程中调用fork()后跟abort()-有关详细信息,请参阅此答案

回答by Suvesh Pratapa

As listed in the signal manpage, any signal with the action listed as 'core' will force a core dump. Some examples are:

信号联机帮助页中所列,任何将操作列为“核心”的信号都将强制进行核心转储。一些例子是:

SIGQUIT       3       Core    Quit from keyboard
SIGILL        4       Core    Illegal Instruction
SIGABRT       6       Core    Abort signal from abort(3)
SIGFPE        8       Core    Floating point exception
SIGSEGV      11       Core    Invalid memory reference

Make sure that you enable core dumps:

确保启用核心转储:

ulimit -c unlimited

回答by Jonathan Leffler

#include <stdlib.h>   // C
//#include <cstdlib>  // C++

void core_dump(void)
{
    abort();
}

回答by Eugene Yokota

You can use kill(2)to send signal.

您可以使用kill(2)发送信号。

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

So,

所以,

kill(getpid(), SIGSEGV);

回答by smcameron

Invoke

调用

abort();

Related, sometimes you'd like a back trace without an actual core dump, and allow the program to continue running: check out glibc backtrace() and backtrace_symbols() functions: http://www.gnu.org/s/libc/manual/html_node/Backtraces.html

相关的,有时你想要一个没有实际核心转储的回溯,并允许程序继续运行:检查 glibc backtrace() 和 backtrace_symbols() 函数:http: //www.gnu.org/s/libc/手册/html_node/Backtraces.html

回答by ephemient

A few years ago, Google released the coredumperlibrary.

几年前,谷歌发布了coredumper库。

Overview

The coredumper library can be compiled into applications to create core dumps of the running program -- without terminating. It supports both single- and multi-threaded core dumps, even if the kernel does not natively support multi-threaded core files.

Coredumper is distributed under the terms of the BSD License.

Example

This is by no means a complete example; it simply gives you a feel for what the coredumper API looks like.

#include <google/coredumper.h>
...
WriteCoreDump('core.myprogram');
/* Keep going, we generated a core file,
 * but we didn't crash.
 */

概述

coredumper 库可以编译到应用程序中以创建正在运行的程序的核心转储——无需终止。它支持单线程和多线程核心转储,即使内核本身不支持多线程核心文件。

Coredumper 根据 BSD 许可条款分发。

例子

这绝不是一个完整的例子;它只是让您了解 coredumper API 的样子。

#include <google/coredumper.h>
...
WriteCoreDump('core.myprogram');
/* Keep going, we generated a core file,
 * but we didn't crash.
 */

It's not what you were asking for, but maybe it's even better :)

这不是您要的,但也许更好:)

回答by sigjuice

#include <assert.h>
.
.
.
     assert(!"this should not happen");

回答by rka444

Sometimes it may be appropriate to do something like this:

有时做这样的事情可能是合适的:

int st = 0;
pid_t p = fork();

if (!p) {
    signal(SIGABRT, SIG_DFL);
    abort(); // having the coredump of the exact copy of the calling thread
} else {
    waitpid(p, &st, 0); // rip the zombie
}

// here the original process continues to live

One problem with this simple approach is that only one thread will be coredumped.

这种简单方法的一个问题是只有一个线程会被核心转储。

回答by Zonk

Another way of generating a core dump:

生成核心转储的另一种方法:

$ bash
$ kill -s SIGSEGV $$

Just create a new instance of the bash and kill it with specified signal. The $$is the PID of the shell. Otherwise you are killing your current bash and will be logged out, terminal closed or disconnected.

只需创建一个新的 bash 实例并用指定的信号杀死它。的$$是外壳的PID。否则,您将杀死当前的 bash,并将被注销、终端关闭或断开连接。

$ bash 
$ kill -s SIGABRT $$
$ bash
$ kill -s SIGFPE $$

回答by karthik339

 #include <stdio.h>
 #include <stdlib.h>
 int main()
 {
   printf("\n");
   printf("Process is aborting\n");
   abort();
   printf("Control not reaching here\n");
   return 0;
 }

use this approach wherever you want :)

在任何地方使用这种方法:)