C 程序如何在不终止的情况下生成自身的核心转储? [英] How can a C program produce a core dump of itself without terminating?

查看:36
本文介绍了C 程序如何在不终止的情况下生成自身的核心转储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 C 程序在某些情况下产生一个核心转储.这是一个在生产环境中运行的程序,不容易停止和重新启动以调整其他类型的调试代码.另外,由于它在生产环境中,我不想调用 abort().在非生产环境中不容易复制正在调查的问题.我希望程序在检测到某些问题时自行生成核心转储,最好提供足够的信息来重命名文件,然后继续.

I want a C program to produce a core dump under certain circumstances. This is a program that runs in a production environment and isn't easily stopped and restarted to adjust other kinds of debugging code. Also, since it's in a production environment, I don't want to call abort(). The issues under investigation aren't easily replicated in a non-production environment. What I'd like is for the program, when it detects certain issues, to produce a core dump on its own, preferably with enough information to rename the file, and then continue.

推荐答案

void create_dump(void)
{
    if(!fork()) {
        // Crash the app in your favorite way here
        *((void*)0) = 42;
    }
}

fork 进程然后让子进程崩溃——它会在你需要的时候给你一个快照

Fork the process then crash the child - it'll give you a snapshot whenever you want

这篇关于C 程序如何在不终止的情况下生成自身的核心转储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆