What is a good way to dump a Linux core file from inside a process?

前端 未结 8 1995
醉梦人生
醉梦人生 2020-12-08 23:14

We have a server (written in C and C++) that currently catches a SEGV and dumps some internal info to a file. I would like to generate a core file and write it to disk at th

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 23:32

    I assume you have a signal handler that catches SEGV, for example, and does something like print a message and call _exit(). (Otherwise, you'd have a core file in the first place!) You could do something like the following.

    void my_handler(int sig)
    {
       ...
       if (wantCore_ && !fork()) {
          setrlimit(...);  // ulimit -Sc unlimited
          sigset(sig, SIG_DFL);  // reset default handler
          raise(sig);  // doesn't return, generates a core file
       }
       _exit(1);
    }
    

提交回复
热议问题