Core Dumps, After the Fact
Outcome: you can examine a crash that already happened — no debugger attached at the time, nobody watching — by opening the file the crash left behind, and you know the two arrangements that must be made before the crash for the file to exist. This chapter is also the guide's most honest about its lab: the sandbox refused one of the arrangements outright, and the refusal is in the transcript where a success would have gone.
What a core dump is
A core dump (or core file) is a copy of a process's memory at the moment it died, written to disk by the operating system: every variable, every frame of the stack, exactly as they stood. Opening one in GDB —
gdb ./program core
— puts you back inside the crash with
the program frozen at the fault. You
cannot run or step — the patient is
not alive — but backtrace, frame,
print and info locals all work,
because they are questions about a
moment, and the moment is preserved.
For a crash that happens once, in
production, at 2 a.m., this is often
the only examination there will ever
be.
Arrangement one: the pattern
Where core files go is set by the kernel's core pattern:
$ cat /proc/sys/kernel/core_pattern
core
The lab machine's answer, core, is the
old, simple arrangement: a file named
core in the dead program's working
directory. Many modern systems answer
with a pipe to a handler instead
(|/usr/lib/systemd/systemd-coredump is
common), in which case cores are
collected by that service and retrieved
with its tools, coredumpctl on
systemd machines. Either way the file
is the same kind of evidence; only the
filing differs. Reading the pattern on
your machine takes one command and
tells you where to look after a crash.
Arrangement two: the limit — and the lab's refusal
Core files are large — a whole process, in a file — so the system rations them with a size limit, often zero. The shell shows and sets it:
ulimit -c unlimited
On the lab machine, that command is refused:
$ ulimit -c unlimited
bash: line 1: ulimit: core file size: cannot modify limit: Operation not permitted
The sandbox sets a hard limit of zero and does not let its tenants raise it. The lab verified the consequence rather than assuming it: it ran the crashing fixture (the shell duly reported the segmentation fault) and looked for the file the pattern promised —
$ ls core
ls: cannot access 'core': No such file or directory
No core, because the size limit forbade
it, whatever the pattern said. The
teaching survives the refusal, and it is
the chapter's real lesson: on a normal
machine the ulimit command works —
run it in the shell or service that
launches your program, before the crash,
and check the pattern while you are
there. Core dumps are arranged in
advance or not at all. A reader whose
machine permits it will get the file the
lab could not; the examination below is
the same either way, because the lab
found a second route to a core file.
The detour: gcore
GDB itself can write a core file from a
program it is holding stopped —
gcore FILE — regardless of the
kernel's arrangements. Mid-session on
basket, stopped at the conditional
breakpoint from chapter 6:
(gdb) print total
$1 = 409
(gdb) gcore basket.core
Saved corefile basket.core
(gcore also exists as a shell command
that does the same to a running process
by number; the lab used GDB's own.) The
warning printed above Saved in the
transcript — one page of a system region
could not be read — is normal and
harmless: that page is a kernel doorway,
not the program's data, and everything
of the program's is in the file.
Days or seconds later, the examination:
$ gdb -q ./basket basket.core -batch -ex 'print i' -ex 'print total' -ex backtrace
Core was generated by `.../basket'.
Program terminated with signal SIGTRAP, Trace/breakpoint trap.
#0 main () at basket.c:23
$1 = 2
$2 = 409
The core remembers the moment exactly:
i was 2, total was 409, the program
stood at line 23. (The "signal SIGTRAP"
is bookkeeping — this core was taken at
a breakpoint, not a crash; a crash's
core names its real signal, as chapter
5's did.) A kernel-made core of store
would open the same way onto the
segfault's own frame, with slot at
0x0 waiting to be printed.
The responsibility in the file
A core file is the program's whole memory — for many programs that includes passwords, session keys, and other people's data, in clear form, in a file that gets copied into bug reports and attached to tickets. Chapter 1's rule applies to cores with interest: treat them as secrets, keep them on machines they belong to, delete them when the question is answered, and never open one that is not yours to inspect.
Check: transcript
gdb-lab/transcripts/t07-core-dumps.txt
holds the whole chapter in order — the
pattern, the refused limit, the crash
that left nothing, the gcore session,
and the post-mortem read-back — the
lab's real experience, unedited.