How can a C program produce a core dump of itself without terminating?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 11:34:48
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

Another way might be to use the Google Coredumper library. This creates a similar result to the fork+abort technique but plays nicer with multithreaded apps (suspends all threads for a little while before forking so that they don't make a mess in the child).

Example:

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

Sun describes how to get a core file on Solaris, HP-UX, Redhat, and Windows here.

Solaris has the gcore program. HP-UX may have it. Otherwise use gdb and its gcore commmand. Windows has win-dbg-root\tlist.exe and win-dbg-root\adplus.vbs

njsf

Do you really want a core, or just a stacktrace ? If all you want is a stacktrace you could take a look at the opensource here and try and integrate the code from there, or maybe just calling it from the command line is enough.

I believe some code in the gdb project might also be useful.

Another think you might want to do is to use gdb to attach to a running process.

$ gdb /path/to/exec 1234 # 1234 is the pid of the running process
Chris

The source code to produce a core dump is in 'gcore', which is part of the gdb package.

Also, the Sun has gcore.

Also, you have to have a separate process running the core dump, as the current process must be suspended. You'll find the details in the gcore source, or you can just run your platform's gcore with your process as the target.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!