简体   繁体   English

系统调用核心转储?

[英]System call for a core dump?

i have a question: exists any system call for generate a core dump? 我有一个问题:是否存在生成核心转储的系统调用?

I know which a core dump could be generated by a signal, but i want know if it's possible generated from system call 我知道哪个核心转储可以由信号生成,但我想知道它是否可能是从系统调用生成的

void createdump(void)
{
    if(!fork()) { //child process
        // Crash the app
        abort() || (*((void*)0) = 42);
    }
}

What ever place you wan't to dump call the function. 什么地方你不想转储称功能。 This will create a child and crash it. 这将创建一个孩子并使其崩溃。 So you can get dump even without exiting your program 所以你甚至可以在不退出程序的情况下获得转储

You can also raise the SIGABRT signal to get the core dump. 您还可以提升SIGABRT信号以获取核心转储。

raise(SIGABRT); 提高(SIGABRT);

*this is equivalent to calling abort() directly. *这相当于直接调用abort()。

Same idea as code hacker's answer , but compilable, with error handling, and without the zombie: 代码黑客的答案相同的想法,但可编译,错误处理,没有僵尸:

#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int 
coredump(void)
{
    pid_t p,r;
    if(0>(p=fork()))
       return -1;
    if(0==p)
        abort() /*child*/;
    /*reap zombie*/
    do r=waitpid(p,0,0); while(0>r && EINTR==errno);
    if(0>r) { 
       perror("waitpid shouldn't have failed");
       abort();
    }
    return 0;
}

This still has the rather obvious deficiency in that that it won't work with multithreaded processes. 这仍然有一个相当明显的缺陷,即它不适用于多线程进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM