I recently encountered a problem with a program that simply crashed when I tried to run it after it had been updated, unfortunately it didn’t give any indication what caused the crash other than the error message ‘Segmentation Fault‘
When reporting the issue the developer asked me for the dump file, and I quickly discovered there wasn’t one – it turns out that a core dump isn’t enabled by default. So I’ve used the example program from the Wikipedia article to demonstrate the steps needed to obtain one.
Note – You need to be careful who you give copies of your core dump files, they contain a LOT of information.
/* * gcc-segfault.c * * Demonstrates how to generate a segmentation fault but attempting to * modify a read only memory location. * * Based on https://en.wikipedia.org/wiki/Segmentation_fault#Examples * Compile with 'gcc -o gcc-segfault gcc-segfault.c' */ int main(void) { char *s = "hello world"; *s = 'H'; }
If we just compile and run this program it results in a segfault but no core dump is produced.
$ gcc -o gcc-segfault gcc-segfault.c
$ ls
gcc-num-format gcc-segfault gcc-segfault.c makefile
$ ./gcc-segfault
Segmentation fault
Displaying the user limits shows why.
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 31642
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 31642
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
The core dump file size is limited to zero blocks. Changing this will allow a core dump file to be written to disk if the program segfaults.
$ ulimit -c unlimited
$ ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 31642
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 31642
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
Running the program again will result in a core dump.
$ ./gcc-segfault Segmentation fault (core dumped) $ ls core gcc-segfault gcc-segfault.c makefile
If you can't locate the core dump file then your system may be configured to use a different filename.
$ sudo sysctl kernel.core_pattern Password: kernel.core_pattern = core $