Part 6 of 7 · 3 min read
Core dumps
Core dumps
Think of a photograph of the scene of an accident, taken before anything is moved. Investigators can study it for days. The analogy stops in one place: a photograph shows what the eye could see, and a core file holds every byte of the process's memory.
In plain words: A core dump is a file that holds the memory and registers of a process at the moment it was ended by a signal such as SIGSEGV. It lets a developer study a crash that happened on another machine or at night. Core dumps are off by default for most users: the limit ulimit -c is 0 on the test machine, and the name pattern is set in /proc/sys/kernel/core_pattern (here core, a file in the current directory). Many systems send dumps to a service instead, for example systemd-coredump.
A worked example. With the limit raised in a subshell:
$ (ulimit -c unlimited; ./crash)
Segmentation fault (core dumped)
$ ls -l core
-rw------- 1 root root 339968 ... core
$ gdb -batch -ex bt ./crash core
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000055a161b79180 in sum (n=0x0) at crash.c:8
#1 0x000055a161b791e8 in main () at crash.c:16
The core file has 339,968 bytes and gives the same backtrace as the live run, without running the program again. A core file can contain passwords and other secrets that were in memory, so it is readable only by its owner (mode 600) and must be handled like the data it came from (Level 10).
The key lines. For crashes that happen in production, enable core dumps, keep the program binary with its debug symbols for the same build, and open the pair with gdb.
Core dump
A core dump is a file that records the memory image and register state of a process when it ends abnormally, so that a debugger can examine the program as it was at the moment of the crash.
Example: gdb ./crash core opens the crash of crash from its core file.
Interview angle
A common question is "What is the difference between kill -9 and kill -15?" A good answer says that SIGTERM (15) can be caught, so the program can clean up, and SIGKILL (9) cannot be caught and ends the process at once, so use SIGTERM first. Another is "What does a segmentation fault mean?" A good answer says that the program touched memory it may not use, the kernel sent SIGSEGV, and a backtrace in gdb finds the line.
Check yourself: 1. Why are core dumps off by default? 2. What do you need besides the core file to read it? 3. Why must a core file be protected?
Words you will meet
| Word | Meaning |
|---|---|
| Signal | A short message to a process, with a number. |
| Handler | A function that runs when a signal arrives. |
| Async-signal-safe | Safe to call inside a handler. |
| Segmentation fault | A bad memory access, signalled by SIGSEGV. |
| Backtrace | The chain of active function calls. |
| Core dump | A file with the memory of a crashed process. |
Quick notes
- Exit status after a signal is 128 plus the signal number: 130 (INT), 137 (KILL), 139 (SEGV), 143 (TERM).
SIGKILLandSIGSTOPcannot be caught. TrySIGTERMfirst.- A handler should only set a flag.
printfandmallocare not safe in one. - A null pointer faults because address 0 is never mapped, and the kernel sends
SIGSEGV. gcc -g -O0, thengdb -batch -ex run -ex btshows the line and the call chain.ulimit -c unlimitedgives a core file (339,968 bytes in the test), andgdb ./prog corereads it.