Skip to content

Commit f02d06b

Browse files
Improve the documentation for generating core dumps
1 parent 872ed6b commit f02d06b

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

doc/21-development.md

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -267,73 +267,90 @@ $3 = std::vector of length 11, capacity 16 = {{static NPos = 1844674407370955161
267267

268268
### Core Dump <a id="development-debug-core-dump"></a>
269269

270-
When the Icinga 2 daemon crashes with a `SIGSEGV` signal
271-
a core dump file should be written. This will help
272-
developers to analyze and fix the problem.
270+
When the Icinga 2 daemon crashes with a `SIGSEGV` signal a core dump file should be written. This will help developers to analyze and fix the problem.
273271

274-
#### Core Dump File Size Limit <a id="development-debug-core-dump-limit"></a>
272+
#### Core Dump Kernel Pattern <a id="development-debug-core-dump-format"></a>
275273

276-
This requires setting the core dump file size to `unlimited`.
274+
Core dumps are generated according to the format specified in `/proc/sys/kernel/core_pattern`.
275+
This can either be a path relative to the directory the program was started in, an absolute path or a pipe to a different program.
277276

277+
For more information see the [core(5)](https://man7.org/linux/man-pages/man5/core.5.html) man page.
278278

279-
##### Systemd
279+
#### Systemd Coredumpctl <a id="development-debug-core-dump-systemd"></a>
280280

281+
Most distributions offer systemd's coredumpctl either by default or as a package. Distributions that offer it by default include RHEL and SLES, on others like Debian or Ubuntu it can be installed via the `systemd-coredump` package. When set up correctly, `core_pattern` will look something like this:
282+
```
283+
# cat /proc/sys/kernel/core_pattern
284+
|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h`
281285
```
282-
systemctl edit icinga2.service
283286

284-
[Service]
285-
...
286-
LimitCORE=infinity
287+
You can look at the generated core dumps with the `coredumpctl list` command. You can show information, including a stack-trace using `coredumpctl show icinga2 -1` and retrieve the actual core-dump file with `coredumpctl dump icinga2 -1 --output <file>`.
287288

288-
systemctl daemon-reload
289+
#### Ubuntu Apport <a id="development-debug-core-dump-apport"></a>
289290

290-
systemctl restart icinga2
291+
Ubuntu uses their own application `apport` to record core dumps. When it is enabled your `core_pattern` will look like this:
292+
```
293+
# cat /proc/sys/kernel/core_pattern
294+
|/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -- %E
291295
```
292296

293-
##### Init Script
297+
Apport is unsuitable for development work, because by default it only works with Ubuntu packages and it has a rather complicated interface for retrieving the core dump.
294298

299+
To set it up to work with unpackaged programs, add the following (create the file if it doesn't exist) to `/etc/apport/settings`:
295300
```
296-
vim /etc/init.d/icinga2
297-
...
298-
ulimit -c unlimited
299-
300-
service icinga2 restart
301+
[main]
302+
unpackaged=true
303+
```
304+
and restart Apport:
305+
```
306+
systemctl restart apport.service
301307
```
302308

303-
##### Verify
309+
When the program crashes you can then find an Apport crash report in `/var/crash/` that you can read with the interactive `apport-cli` command. To extract the core dump you run `apport-unpack /var/crash/<crash-file> <output-dir>` which then saves a `<outputdir>/CoreDump` file that contains the actual core dump.
304310

305-
Verify that the Icinga 2 process core file size limit is set to `unlimited`.
311+
Unless you rely on Apport for some other workflow, systemd's coredumpctl is a much better option and is available on Ubuntu in the `systemd-coredump` package that can replace Apport on your system with no further setup required.
306312

307-
```
308-
for pid in $(pidof icinga2); do cat /proc/$pid/limits; done
313+
#### Directly to a File <a id="development-debug-core-dump-direct"></a>
309314

310-
...
311-
Max core file size unlimited unlimited bytes
315+
If coredumpctl is not available, simply writing the core dump directly to a file is also sufficient. You can set up your `core_pattern` to write a file to a suitable path:
316+
317+
```bash
318+
sysctl -w kernel.core_pattern=/var/lib/cores/core.%e.%p.%h.%t
319+
install -m 1777 -d /var/lib/cores
312320
```
313321

322+
If you want to make this setting permanent you can also add a file to `/etc/sysctl.d`, named something like `80-coredumps.conf`:
323+
```
324+
kernel.core_pattern = /var/lib/cores/core.%e.%p.%h.%t
325+
```
314326

315-
#### Core Dump Kernel Format <a id="development-debug-core-dump-format"></a>
327+
This will create core dump files in `/var/lib/cores` where `%e` is the truncated name of the program, `%p` is the programs PID, `%h` is the hostname, and `%t` a timestamp.
316328

317-
The Icinga 2 daemon runs with the SUID bit set. Therefore you need
318-
to explicitly enable core dumps for SUID on Linux.
329+
Note that unlike the other methods this requires the core size limit to be set for the process. When starting Icinga 2 via systemd you can set it to unlimited by adding the following to `/etc/systemd/system/icinga2.service.d/limits.conf`:
330+
```
331+
[Service]
332+
LimitCORE=infinity
333+
```
319334

335+
When using an init script or starting manually you need to run `ulimit -c unlimited` before starting the program:
320336
```bash
321-
sysctl -w fs.suid_dumpable=2
337+
ulimit -c unlimited
338+
./icinga2 daemon
322339
```
323340

324-
Adjust the coredump kernel format and file location on Linux:
325-
341+
To verify that the limit has been set to `unlimited` run the following:
326342
```bash
327-
sysctl -w kernel.core_pattern=/var/lib/cores/core.%e.%p
328-
329-
install -m 1777 -d /var/lib/cores
343+
for pid in $(pidof icinga2); do cat /proc/$pid/limits; done
344+
```
345+
And look for the line:
346+
```
347+
Max core file size unlimited unlimited bytes
330348
```
331349

332-
MacOS:
350+
#### MacOS <a id="development-debug-core-dump-macos"></a>
333351

334352
```bash
335353
sysctl -w kern.corefile=/cores/core.%P
336-
337354
chmod 777 /cores
338355
```
339356

0 commit comments

Comments
 (0)