33

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
  • 44,692
  • 7
  • 66
  • 118
Nektarios
  • 10,173
  • 8
  • 63
  • 93

6 Answers6

49

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
  • 42,753
  • 9
  • 87
  • 112
  • 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
8

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

Aadishri
  • 1,341
  • 2
  • 18
  • 26
3

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

3

I had no luck with kill -QUIT in a particular case. What worked for me is:

gdb --pid <process_id> -ex "set confirm off" -ex generate-core-file -ex q
Pedro Gimeno
  • 2,837
  • 1
  • 25
  • 33
1

On sles12.. Below code worked for me :

kill -11

The previous suggestions did not do anything.

LuFFy
  • 8,799
  • 10
  • 41
  • 59
Marvin
  • 11
  • 1
0

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

Maik Ro
  • 320
  • 1
  • 11