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

查看:181
本文介绍了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
        abort() || (*((void*)0) = 42);
    }
}

叉过程然后让孩子崩溃 - 给你一个快照,只要你想要

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

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

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