What is the correct way to force an app to core dump and quit?

前端 未结 5 1722
醉梦人生
醉梦人生 2021-01-12 19:19

I just came across some code which used the kill system call to send a SIGSEGV signal to an app. The rationale behind this was that this would force the app to core dump and

相关标签:
5条回答
  • 2021-01-12 19:44

    SIGQUIT is the correct signal to send to a program if you wish to produce a core dump. kill is the correct command line program to send signals (it is of course poorly named, since not all signals will kill the program).

    Note, you should not send random signals to the program, not all of them will produce a core dump. Many of them will be handled by the program itself, either consumed, ignored, or induce other processing. Thus sending a SIGSEGV is wrong.


    GCC Says: http://www.gnu.org/s/libc/manual/html_node/Termination-Signals.html

    POSIX/Unix Says: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/signal.h.html

    0 讨论(0)
  • 2021-01-12 19:56

    Yes. kill is somewhat misnamed -- it can send any signal. There are many uses for kill which don't result in the process being killed at all!

    0 讨论(0)
  • 2021-01-12 19:58

    The way to do it in Unix/Linux is to call abort() which will send SIGABORT to current process. The other option is raise() where you can specify what signal you want to send to current process.

    0 讨论(0)
  • 2021-01-12 19:58

    Richard Stevens (_Advanced Programming in the UNIX Environment) wrote:

    The generation of core is an implementation features of most Unix. It is not part of POSIX.1.

    He lists 12 signals whose default action is to terminate with a core (ANSI: SIGABRT, SIGFPE, SIGILL, SIGSEGV, POSIX: SIGQUIT, Other: SIGBUS, SIGEMT, SIGIOT, SIGSYS, SIGTRAP, SIGXCPU, SIGXFSZ), all of them are overwritable (the two signals which aren't overwritable are SIGKILL and SIGSTOP).

    I've never seen a way to generate a core which isn't the use of a default signal handler.

    So if your goal is to generate a core and stop, the best is to choose a signal whose default handler does the job (SIGSEGV does the job), reset the default handler for the signal if you are using it and then use kill.

    0 讨论(0)
  • 2021-01-12 20:02

    If you want to make an application dump it's core from another program, pretty much the only way to do it is via a signal. SEGV would be fine for this. Alternatively you can hook a debugger up to the program and freeze it and view it's registers and such without killing it.

    If you want to dump a core from within an application there are nicer ways to do it, like via an assert().

    So, no, it's not particularly wrong to send a SEGV to a program. You could also send things like SIGILL for illegal instruction, or a divide by zero signal. It's all fine.

    0 讨论(0)
提交回复
热议问题