29

I'm writing error handling code for a server on FreeBSD. For extremely serious errors I want to avoid data corruption by immediately terminating. That's easy, exit(3). Before I exit, I output my relevant variables that led me there. However, ideally, this termination would be accompanied by a .core so that I could fully investigate what got me to this catastrophic (and likely hard to reproduce) state.

How can I force this to happen?

Deduplicator
  • 43,322
  • 6
  • 62
  • 109
Nektarios
  • 9,853
  • 8
  • 60
  • 93

5 Answers5

41

kill -QUIT process_id will cause a core dump from a running process (assuming that resource limits allow it).

Or see man 3 abort for causing a program to dump itself.

Added: From an interactive shell, a running program can be made to abort with the quit key, usually Ctrl+\, which sends a SIGQUIT just as the more common Ctrl+C sends a SIGINT. This is identical to the kill -QUIT… it's just easier to type if you are on the controlling terminal. See man 1 stty if your default quit key is different.

msw
  • 41,609
  • 8
  • 82
  • 107
  • 1
    You can also use kill -3 [pid] - same deal, less typing. You can see the other kill flags on the manpage: man kill. – smcphill Jul 03 '11 at 05:13
  • Well, I want to do it from the process itself; so `abort(3)` seems ok but from some testing here I don't seem to be able to access any symbols when examining the resulting `.core`. I can get a backtrace fine, but if I try to print any of my variables they aren't found. Is this expected with `abort(3)` or am I doing something stupid? `gcc -g -O0 test.c -o test` then `./test` then `gdb ./test test.core` – Nektarios Jul 03 '11 at 05:26
  • 2
    I think your problem is with using gdb: you need to pick a frame from the `where` backtrace to give scope to the variable names. See http://sourceware.org/gdb/current/onlinedocs/gdb/Selection.html#Selection – msw Jul 04 '11 at 01:36
6

This helped me! kill -11 always works for me. 11 is SIGSEGV (invalid memory reference)

Aadishri
  • 1,231
  • 2
  • 18
  • 26
2

You might also want to take a look at gcore(1) (http://man.freebsd.org/gcore).

1

On sles12.. Below code worked for me :

kill -11

The previous suggestions did not do anything.

LuFFy
  • 7,689
  • 10
  • 38
  • 58
Marvin
  • 11
  • 1
0

You could also try kill -BUS <pid> which worked for me (though on ubuntu 20.04)

Maik Ro
  • 300
  • 1
  • 11