I have a process in Linux that\'s getting a segmentation fault. How can I tell it to generate a core dump when it fails?
As explained above the real question being asked here is how to enable core dumps on a system where they are not enabled. That question is answered here.
If you've come here hoping to learn how to generate a core dump for a hung process, the answer is
gcore <pid>
if gcore is not available on your system then
kill -ABRT <pid>
Don't use kill -SEGV as that will often invoke a signal handler making it harder to diagnose the stuck process
There are more things that may influence the generation of a core dump. I encountered these:
/proc/sys/kernel/core_pattern
. /proc/sys/fs/suid_dumpable
may prevent the core to be generated.There are more situations which may prevent the generation that are described in the man page - try man core
.
By default you will get a core file. Check to see that the current directory of the process is writable, or no core file will be created.
Ubuntu 19.04
All other answers themselves didn't help me. But the following sum up did the job
Create ~/.config/apport/settings
with the following content:
[main]
unpackaged=true
(This tells apport to also write core dumps for custom apps)
check: ulimit -c
. If it outputs 0, fix it with
ulimit -c unlimited
Just for in case restart apport:
sudo systemctl restart apport
Crash files are now written in /var/crash/
. But you cannot use them with gdb. To use them with gdb, use
apport-unpack <location_of_report> <target_directory>
Further information:
core_pattern
. Be aware, that that file might get overwritten by the apport service on restarting.ulimit -c
value might get changed automatically while you're trying other answers of the web. Be sure to check it regularly during setting up your core dump creation.References:
What I did at the end was attach gdb to the process before it crashed, and then when it got the segfault I executed the generate-core-file
command. That forced generation of a core dump.
It's worth mentioning that if you have a systemd set up, then things are a little bit different. The set up typically would have the core files be piped, by means of core_pattern
sysctl value, through systemd-coredump(8)
. The core file size rlimit would typically be configured as "unlimited" already.
It is then possible to retrieve the core dumps using coredumpctl(1)
.
The storage of core dumps, etc. is configured by coredump.conf(5)
. There are examples of how to get the core files in the coredumpctl man page, but in short, it would look like this:
Find the core file:
[vps@phoenix]~$ coredumpctl list test_me | tail -1
Sun 2019-01-20 11:17:33 CET 16163 1224 1224 11 present /home/vps/test_me
Get the core file:
[vps@phoenix]~$ coredumpctl -o test_me.core dump 16163