开发者

Dump core if SIGSEGV (in C)?

How can I dump the core when my program receives the SIGSEGV signal ? (The server that runs my program has very limited permissions and therefore core dump is disabled by default.)

I have written the following using gcore but I would like to use C functions instead. Can I som开发者_JAVA技巧ehow catch the core and write it to a folder somewhere ?


void segfaulthandler(int parameter)
{
    char gcore[50];
    sprintf(gcore, "gcore -s -c \"core\" %u", getpid());
    system(gcore);

    exit(1);
}

int main(void)
{
    signal(SIGSEGV, segfaulthandler);
}


Unless there's a hard limit preventing you, you could use setrlimit(RLIMIT_CORE, ...) to increase the softlimit and enable coredumps - this corresponds to running ulimit -c in shell.


On linux, you typically can do:

$ ulimit -c unlimited

The resulting core file will be written in the current working directory of the process when the signal is received.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜