I want a function in C on linux to collect core du

2020-05-03 12:17发布

问题:

abort() do collect the core dump, but I don't want the process to terminate. dump_core() collects the core dump, but in kernel space. Is there any function equivalent to dump_core() in user space?

回答1:

A simple way to do it yourself is to fork the process (which creates a complete copy of the parent process) and call abort from the child process.

The child process will be aborted with a core-dump, while the parent process continues as if nothing happened.



回答2:

Use gcore.

    .
    .
    .
char command[ 1024 ];
sprintf( command, "gcore -o /core/file/name %d", getpid() );
system( command );
    .
    .
    .

Error and bounds checking are omitted.



回答3:

There is no such Linux C command. However, you may find some third party tools that can do this for you. For example, Google coredumper, which is also supposed to be able to capture all the threads. Another way would be to attach gdb to your running process, and issue the gcore command. This is essentially what the gcore command line utility does.



回答4:

Kernel generates SIGSEGV signal to the process whenever coredumps, I think you should attach a handler to the SIGSEGV signal(Link) and call fork from that handler function.



标签: c linux core