如何以编程方式在 C/C 中引发核心转储++

发布于 2024-07-24 06:10:41 字数 178 浏览 13 评论 0原文

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

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

int * crash = NULL;
*crash = 1;

但我想知道是否有更干净的方法?

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

蓝礼 2024-07-31 06:10:41

提高信号编号 6(Linux 中的SIGABRT)是实现此目的的一种方法(但请记住,在所有 POSIX 实现中,SIGABRT 并不要求为 6,因此您可以如果这不是快速的脏调试代码,则希望使用 SIGABRT 值本身)。

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

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

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

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

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

蘸点软妹酱 2024-07-31 06:10:41

几年前,Google 发布了 coredumper 库。

概述

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

Coredumper 是根据 BSD 许可证条款分发的。

示例

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

#include ; 
  ... 
  WriteCoreDump('core.myprogram'); 
  /* 继续,我们生成了一个核心文件, 
   * 但我们没有崩溃。 
   */ 
  

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

A few years ago, Google released the coredumper library.

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.
 */

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

︶葆Ⅱㄣ 2024-07-31 06:10:41

信号手册页中列出的,具有列出的操作的任何信号因为“核心”将强制进行核心转储。 一些示例是:

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

确保启用核心转储:

ulimit -c unlimited

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
何其悲哀 2024-07-31 06:10:41
#include <stdlib.h>   // C
//#include <cstdlib>  // C++

void core_dump(void)
{
    abort();
}
#include <stdlib.h>   // C
//#include <cstdlib>  // C++

void core_dump(void)
{
    abort();
}
薄荷港 2024-07-31 06:10:41

调用

abort();

相关,有时您希望在没有实际核心转储的情况下进行回溯跟踪,并允许程序继续运行:查看 glibc backtrace() 和 backtrace_symbols() 函数:
http://www.gnu.org/s/libc/manual/ html_node/Backtraces.html

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

喵星人汪星人 2024-07-31 06:10:41

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

$ bash
$ kill -s SIGSEGV $

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

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

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 
$ kill -s SIGABRT $
$ bash
$ kill -s SIGFPE $
烂人 2024-07-31 06:10:41

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

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

所以,

kill(getpid(), SIGSEGV);

You can use kill(2) to send signal.

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

So,

kill(getpid(), SIGSEGV);
ヤ经典坏疍 2024-07-31 06:10:41

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

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

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

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.

貪欢 2024-07-31 06:10:41
 #include <stdio.h>
 #include <stdlib.h>
 int main()
 {
   printf("\n");
   printf("Process is aborting\n");
   abort();
   printf("Control not reaching here\n");
   return 0;
 }

任何你想要的地方都可以使用这种方法:)

 #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 :)

护你周全 2024-07-31 06:10:41
#include <assert.h>
.
.
.
     assert(!"this should not happen");
#include <assert.h>
.
.
.
     assert(!"this should not happen");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文