CONTRIBUTING.d/patches: Please provide a git-range-diff(1)
[man-pages.git] / man5 / core.5
blob2fb7979da21a486157d39b9ad98f57062ef4e53f
1 .\" Copyright (c) 2006, 2008 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH core 5 (date) "Linux man-pages (unreleased)"
6 .SH NAME
7 core \- core dump file
8 .SH DESCRIPTION
9 The default action of certain signals is to cause a process to terminate
10 and produce a
11 .IR "core dump file" ,
12 a file containing an image of the process's memory at
13 the time of termination.
14 This image can be used in a debugger (e.g.,
15 .BR gdb (1))
16 to inspect the state of the program at the time that it terminated.
17 A list of the signals which cause a process to dump core can be found in
18 .BR signal (7).
20 A process can set its soft
21 .B RLIMIT_CORE
22 resource limit to place an upper limit on the size of the core dump file
23 that will be produced if it receives a "core dump" signal; see
24 .BR getrlimit (2)
25 for details.
27 There are various circumstances in which a core dump file is
28 not produced:
29 .IP \[bu] 3
30 The process does not have permission to write the core file.
31 (By default, the core file is called
32 .I core
34 .IR core.pid ,
35 where
36 .I pid
37 is the ID of the process that dumped core,
38 and is created in the current working directory.
39 See below for details on naming.)
40 Writing the core file fails if the directory in which
41 it is to be created is not writable,
42 or if a file with the same name exists and
43 is not writable
44 or is not a regular file
45 (e.g., it is a directory or a symbolic link).
46 .IP \[bu]
47 A (writable, regular) file with the same name as would be used for the
48 core dump already exists, but there is more than one hard link to that
49 file.
50 .IP \[bu]
51 The filesystem where the core dump file would be created is full;
52 or has run out of inodes; or is mounted read-only;
53 or the user has reached their quota for the filesystem.
54 .IP \[bu]
55 The directory in which the core dump file is to be created does
56 not exist.
57 .IP \[bu]
58 The
59 .B RLIMIT_CORE
60 (core file size) or
61 .B RLIMIT_FSIZE
62 (file size) resource limits for the process are set to zero; see
63 .BR getrlimit (2)
64 and the documentation of the shell's
65 .I ulimit
66 command
67 .RI ( limit
69 .BR csh (1)).
70 However,
71 .B RLIMIT_CORE
72 will be ignored if the system is configured to pipe core dumps to a program.
73 .IP \[bu]
74 The binary being executed by the process does not have read
75 permission enabled.
76 (This is a security measure to
77 ensure that an executable whose contents are not readable
78 does not produce a\[em]possibly readable\[em]core dump containing
79 an image of the executable.)
80 .IP \[bu]
81 The process is executing a set-user-ID (set-group-ID) program
82 that is owned by a user (group) other than the real user (group)
83 ID of the process,
84 or the process is executing a program that has file capabilities (see
85 .BR capabilities (7)).
86 (However, see the description of the
87 .BR prctl (2)
88 .B PR_SET_DUMPABLE
89 operation, and the description of the
90 .I /proc/sys/fs/suid_dumpable
91 .\" FIXME . Perhaps relocate discussion of /proc/sys/fs/suid_dumpable
92 .\" and PR_SET_DUMPABLE to this page?
93 file in
94 .BR proc (5).)
95 .IP \[bu]
96 .I /proc/sys/kernel/core_pattern
97 is empty and
98 .I /proc/sys/kernel/core_uses_pid
99 contains the value 0.
100 (These files are described below.)
101 Note that if
102 .I /proc/sys/kernel/core_pattern
103 is empty and
104 .I /proc/sys/kernel/core_uses_pid
105 contains the value 1,
106 core dump files will have names of the form
107 .IR .pid ,
108 and such files are hidden unless one uses the
109 .BR ls (1)
110 .I \-a
111 option.
112 .IP \[bu]
113 (Since Linux 3.7)
114 .\" commit 046d662f481830e652ac34cd112249adde16452a
115 The kernel was configured without the
116 .B CONFIG_COREDUMP
117 option.
119 In addition,
120 a core dump may exclude part of the address space of the process if the
121 .BR madvise (2)
122 .B MADV_DONTDUMP
123 flag was employed.
125 On systems that employ
126 .BR systemd (1)
127 as the
128 .I init
129 framework, core dumps may instead be placed in a location determined by
130 .BR systemd (1).
131 See below for further details.
133 .SS Naming of core dump files
134 By default, a core dump file is named
135 .IR core ,
136 but the
137 .I /proc/sys/kernel/core_pattern
138 file (since Linux 2.6 and 2.4.21)
139 can be set to define a template that is used to name core dump files.
140 The template can contain % specifiers which are substituted
141 by the following values when a core file is created:
143 .RS 4
144 .PD 0
145 .TP 4
147 A single % character.
150 Core file size soft resource limit of crashing process (since Linux 2.6.24).
153 .\" Added in git commit 12a2b4b2241e318b4f6df31228e4272d2c2968a1
154 Dump mode\[em]same as value returned by
155 .BR prctl (2)
156 .B PR_GET_DUMPABLE
157 (since Linux 3.7).
160 The process or thread's
161 .I comm
162 value, which typically is the same as the executable filename
163 (without path prefix, and truncated to a maximum of 15 characters),
164 but may have been modified to be something different;
165 see the discussion of
166 .IR /proc/ pid /comm
168 .IR /proc/ pid /task/ tid /comm
170 .BR proc (5).
173 Pathname of executable,
174 with slashes (\[aq]/\[aq]) replaced by exclamation marks (\[aq]!\[aq])
175 (since Linux 3.0).
178 Numeric real GID of dumped process.
181 Hostname (same as \fInodename\fP returned by \fBuname\fP(2)).
184 TID of thread that triggered core dump,
185 as seen in the PID namespace in which the thread resides
186 .\" commit b03023ecbdb76c1dec86b41ed80b123c22783220
187 (since Linux 3.18).
190 TID of thread that triggered core dump, as seen in the initial PID namespace
191 .\" commit b03023ecbdb76c1dec86b41ed80b123c22783220
192 (since Linux 3.18).
195 PID of dumped process,
196 as seen in the PID namespace in which the process resides.
199 .\" Added in git commit 65aafb1e7484b7434a0c1d4c593191ebe5776a2f
200 PID of dumped process, as seen in the initial PID namespace
201 (since Linux 3.12).
204 Number of signal causing dump.
207 Time of dump, expressed as seconds since the
208 Epoch, 1970-01-01 00:00:00 +0000 (UTC).
211 Numeric real UID of dumped process.
215 A single % at the end of the template is dropped from the
216 core filename, as is the combination of a % followed by any
217 character other than those listed above.
218 All other characters in the template become a literal
219 part of the core filename.
220 The template may include \[aq]/\[aq] characters, which are interpreted
221 as delimiters for directory names.
222 The maximum size of the resulting core filename is 128 bytes (64 bytes
223 before Linux 2.6.19).
224 The default value in this file is "core".
225 For backward compatibility, if
226 .I /proc/sys/kernel/core_pattern
227 does not include
228 .I %p
230 .I /proc/sys/kernel/core_uses_pid
231 (see below)
232 is nonzero, then .PID will be appended to the core filename.
234 Paths are interpreted according to the settings that are active for the
235 crashing process.
236 That means the crashing process's mount namespace (see
237 .BR mount_namespaces (7)),
238 its current working directory (found via
239 .BR getcwd (2)),
240 and its root directory (see
241 .BR chroot (2)).
243 Since Linux 2.4, Linux has also provided
244 a more primitive method of controlling
245 the name of the core dump file.
246 If the
247 .I /proc/sys/kernel/core_uses_pid
248 file contains the value 0, then a core dump file is simply named
249 .IR core .
250 If this file contains a nonzero value, then the core dump file includes
251 the process ID in a name of the form
252 .IR core.PID .
254 Since Linux 3.6,
255 .\" 9520628e8ceb69fa9a4aee6b57f22675d9e1b709
257 .I /proc/sys/fs/suid_dumpable
258 is set to 2 ("suidsafe"), the pattern must be either an absolute pathname
259 (starting with a leading \[aq]/\[aq] character) or a pipe, as defined below.
260 .SS Piping core dumps to a program
261 Since Linux 2.6.19, Linux supports an alternate syntax for the
262 .I /proc/sys/kernel/core_pattern
263 file.
264 If the first character of this file is a pipe symbol (\fB|\fP),
265 then the remainder of the line is interpreted as the command-line for
266 a user-space program (or script) that is to be executed.
268 Since Linux 5.3.0,
269 .\" commit 315c69261dd3fa12dbc830d4fa00d1fad98d3b03
270 the pipe template is split on spaces into an argument list
271 .I before
272 the template parameters are expanded.
273 In earlier kernels, the template parameters are expanded first and
274 the resulting string is split on spaces into an argument list.
275 This means that in earlier kernels executable names added by the
276 .I %e
278 .I %E
279 template parameters could get split into multiple arguments.
280 So the core dump handler needs to put the executable names as the last
281 argument and ensure it joins all parts of the executable name using spaces.
282 Executable names with multiple spaces in them are not correctly represented
283 in earlier kernels,
284 meaning that the core dump handler needs to use mechanisms to find
285 the executable name.
287 Instead of being written to a file, the core dump is given as
288 standard input to the program.
289 Note the following points:
290 .IP \[bu] 3
291 The program must be specified using an absolute pathname (or a
292 pathname relative to the root directory, \fI/\fP),
293 and must immediately follow the '|' character.
294 .IP \[bu]
295 The command-line arguments can include any of
296 the % specifiers listed above.
297 For example, to pass the PID of the process that is being dumped, specify
298 .I %p
299 in an argument.
300 .IP \[bu]
301 The process created to run the program runs as user and group
302 .IR root .
303 .IP \[bu]
304 Running as
305 .I root
306 does not confer any exceptional security bypasses.
307 Namely, LSMs (e.g., SELinux) are still active and may prevent the handler
308 from accessing details about the crashed process via
309 .IR /proc/ pid.
310 .IP \[bu]
311 The program pathname is interpreted with respect to the initial mount namespace
312 as it is always executed there.
313 It is not affected by the settings
314 (e.g., root directory, mount namespace, current working directory)
315 of the crashing process.
316 .IP \[bu]
317 The process runs in the initial namespaces
318 (PID, mount, user, and so on)
319 and not in the namespaces of the crashing process.
320 One can utilize specifiers such as
321 .I %P
322 to find the right
323 .IR /proc/ pid
324 directory and probe/enter the crashing process's namespaces if needed.
325 .IP \[bu]
326 The process starts with its current working directory
327 as the root directory.
328 If desired, it is possible change to the working directory of
329 the dumping process by employing the value provided by the
330 .I %P
331 specifier to change to the location of the dumping process via
332 .IR /proc/ pid /cwd .
333 .IP \[bu]
334 Command-line arguments can be supplied to the
335 program (since Linux 2.6.24),
336 delimited by white space (up to a total line length of 128 bytes).
337 .IP \[bu]
339 .B RLIMIT_CORE
340 limit is not enforced for core dumps that are piped to a program
341 via this mechanism.
343 .SS /proc/sys/kernel/core_pipe_limit
344 When collecting core dumps via a pipe to a user-space program,
345 it can be useful for the collecting program to gather data about
346 the crashing process from that process's
347 .IR /proc/ pid
348 directory.
349 In order to do this safely,
350 the kernel must wait for the program collecting the core dump to exit,
351 so as not to remove the crashing process's
352 .IR /proc/ pid
353 files prematurely.
354 This in turn creates the
355 possibility that a misbehaving collecting program can block
356 the reaping of a crashed process by simply never exiting.
358 Since Linux 2.6.32,
359 .\" commit a293980c2e261bd5b0d2a77340dd04f684caff58
361 .I /proc/sys/kernel/core_pipe_limit
362 can be used to defend against this possibility.
363 The value in this file defines how many concurrent crashing
364 processes may be piped to user-space programs in parallel.
365 If this value is exceeded, then those crashing processes above this value
366 are noted in the kernel log and their core dumps are skipped.
368 A value of 0 in this file is special.
369 It indicates that unlimited processes may be captured in parallel,
370 but that no waiting will take place (i.e., the collecting
371 program is not guaranteed access to
372 .IR /proc/<crashing\-PID> ).
373 The default value for this file is 0.
375 .SS Controlling which mappings are written to the core dump
376 Since Linux 2.6.23, the Linux-specific
377 .IR /proc/ pid /coredump_filter
378 file can be used to control which memory segments are written to the
379 core dump file in the event that a core dump is performed for the
380 process with the corresponding process ID.
382 The value in the file is a bit mask of memory mapping types (see
383 .BR mmap (2)).
384 If a bit is set in the mask, then memory mappings of the
385 corresponding type are dumped; otherwise they are not dumped.
386 The bits in this file have the following meanings:
388 .PD 0
389 .RS 4
391 bit 0
392 Dump anonymous private mappings.
394 bit 1
395 Dump anonymous shared mappings.
397 bit 2
398 Dump file-backed private mappings.
400 bit 3
401 Dump file-backed shared mappings.
402 .\" file-backed shared mappings of course also update the underlying
403 .\" mapped file.
405 bit 4 (since Linux 2.6.24)
406 Dump ELF headers.
408 bit 5 (since Linux 2.6.28)
409 Dump private huge pages.
411 bit 6 (since Linux 2.6.28)
412 Dump shared huge pages.
414 bit 7 (since Linux 4.4)
415 .\" commit ab27a8d04b32b6ee8c30c14c4afd1058e8addc82
416 Dump private DAX pages.
418 bit 8 (since Linux 4.4)
419 .\" commit ab27a8d04b32b6ee8c30c14c4afd1058e8addc82
420 Dump shared DAX pages.
424 By default, the following bits are set: 0, 1, 4 (if the
425 .B CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
426 kernel configuration option is enabled), and 5.
427 This default can be modified at boot time using the
428 .I coredump_filter
429 boot option.
431 The value of this file is displayed in hexadecimal.
432 (The default value is thus displayed as 33.)
434 Memory-mapped I/O pages such as frame buffer are never dumped, and
435 virtual DSO
436 .RB ( vdso (7))
437 pages are always dumped, regardless of the
438 .I coredump_filter
439 value.
441 A child process created via
442 .BR fork (2)
443 inherits its parent's
444 .I coredump_filter
445 value;
447 .I coredump_filter
448 value is preserved across an
449 .BR execve (2).
451 It can be useful to set
452 .I coredump_filter
453 in the parent shell before running a program, for example:
455 .in +4n
457 .RB "$" " echo 0x7 > /proc/self/coredump_filter"
458 .RB "$" " ./some_program"
462 This file is provided only if the kernel was built with the
463 .B CONFIG_ELF_CORE
464 configuration option.
466 .SS Core dumps and systemd
467 On systems using the
468 .BR systemd (1)
469 .I init
470 framework, core dumps may be placed in a location determined by
471 .BR systemd (1).
472 To do this,
473 .BR systemd (1)
474 employs the
475 .I core_pattern
476 feature that allows piping core dumps to a program.
477 One can verify this by checking whether core dumps are being piped to the
478 .BR systemd\-coredump (8)
479 program:
481 .in +4n
483 $ \fBcat /proc/sys/kernel/core_pattern\fP
484 |/usr/lib/systemd/systemd\-coredump %P %u %g %s %t %c %e
488 In this case, core dumps will be placed in the location configured for
489 .BR systemd\-coredump (8),
490 typically as
491 .BR lz4 (1)
492 compressed files in the directory
493 .IR /var/lib/systemd/coredump/ .
494 One can list the core dumps that have been recorded by
495 .BR systemd\-coredump (8)
496 using
497 .BR coredumpctl (1):
500 $ \fBcoredumpctl list | tail \-5\fP
501 Wed 2017\-10\-11 22:25:30 CEST  2748 1000 1000 3 present  /usr/bin/sleep
502 Thu 2017\-10\-12 06:29:10 CEST  2716 1000 1000 3 present  /usr/bin/sleep
503 Thu 2017\-10\-12 06:30:50 CEST  2767 1000 1000 3 present  /usr/bin/sleep
504 Thu 2017\-10\-12 06:37:40 CEST  2918 1000 1000 3 present  /usr/bin/cat
505 Thu 2017\-10\-12 08:13:07 CEST  2955 1000 1000 3 present  /usr/bin/cat
508 The information shown for each core dump includes the date and time
509 of the dump, the PID, UID, and GID  of the dumping process,
510 the signal number that caused the core dump,
511 and the pathname of the executable that was being run by the dumped process.
512 Various options to
513 .BR coredumpctl (1)
514 allow a specified coredump file to be pulled from the
515 .BR systemd (1)
516 location into a specified file.
517 For example, to extract the core dump for PID 2955 shown above to a file named
518 .I core
519 in the current directory, one could use:
521 .in +4n
523 $ \fBcoredumpctl dump 2955 \-o core\fP
527 For more extensive details, see the
528 .BR coredumpctl (1)
529 manual page.
531 To (persistently) disable the
532 .BR systemd (1)
533 mechanism that archives core dumps, restoring to something more like
534 traditional Linux behavior, one can set an override for the
535 .BR systemd (1)
536 mechanism, using something like:
538 .in +4n
540 # \fBecho "kernel.core_pattern=core.%p" > \e\fP
541 \fB               /etc/sysctl.d/50\-coredump.conf\fP
542 # \fB/lib/systemd/systemd\-sysctl\fP
546 It is also possible to temporarily (i.e., until the next reboot) change the
547 .I core_pattern
548 setting using a command such as the following
549 (which causes the names of core dump files to include the executable name
550 as well as the number of the signal which triggered the core dump):
552 .in +4n
554 # \fBsysctl \-w kernel.core_pattern="%e\-%s.core"\fP
558 .SH NOTES
560 .BR gdb (1)
561 .I gcore
562 command can be used to obtain a core dump of a running process.
564 In Linux versions up to and including 2.6.27,
565 .\" Changed with commit 6409324b385f3f63a03645b4422e3be67348d922
566 if a multithreaded process (or, more precisely, a process that
567 shares its memory with another process by being created with the
568 .B CLONE_VM
569 flag of
570 .BR clone (2))
571 dumps core, then the process ID is always appended to the core filename,
572 unless the process ID was already included elsewhere in the
573 filename via a
574 .I %p
575 specification in
576 .IR /proc/sys/kernel/core_pattern .
577 (This is primarily useful when employing the obsolete
578 LinuxThreads implementation,
579 where each thread of a process has a different PID.)
580 .\" Always including the PID in the name of the core file made
581 .\" sense for LinuxThreads, where each thread had a unique PID,
582 .\" but doesn't seem to serve any purpose with NPTL, where all the
583 .\" threads in a process share the same PID (as POSIX.1 requires).
584 .\" Probably the behavior is maintained so that applications using
585 .\" LinuxThreads continue appending the PID (the kernel has no easy
586 .\" way of telling which threading implementation the user-space
587 .\" application is using). -- mtk, April 2006
588 .SH EXAMPLES
589 The program below can be used to demonstrate the use of the
590 pipe syntax in the
591 .I /proc/sys/kernel/core_pattern
592 file.
593 The following shell session demonstrates the use of this program
594 (compiled to create an executable named
595 .IR core_pattern_pipe_test ):
597 .in +4n
599 .RB "$" " cc \-o core_pattern_pipe_test core_pattern_pipe_test.c"
600 .RB "$" " su"
601 Password:
602 .RB "#" " echo \[dq]|$PWD/core_pattern_pipe_test %p \
603 UID=%u GID=%g sig=%s\[dq] > \e"
604 .B "    /proc/sys/kernel/core_pattern"
605 .RB "#" " exit"
606 .RB "$" " sleep 100"
607 .BR "\[ha]\e" "                     # type control\-backslash"
608 Quit (core dumped)
609 .RB "$" " cat core.info"
610 argc=5
611 argc[0]=</home/mtk/core_pattern_pipe_test>
612 argc[1]=<20575>
613 argc[2]=<UID=1000>
614 argc[3]=<GID=100>
615 argc[4]=<sig=3>
616 Total bytes in core dump: 282624
619 .SS Program source
622 /* core_pattern_pipe_test.c */
624 #define _GNU_SOURCE
625 #include <sys/stat.h>
626 #include <fcntl.h>
627 #include <limits.h>
628 #include <stdio.h>
629 #include <stdlib.h>
630 #include <unistd.h>
632 #define BUF_SIZE 1024
635 main(int argc, char *argv[])
637     ssize_t nread, tot;
638     char buf[BUF_SIZE];
639     FILE *fp;
640     char cwd[PATH_MAX];
642     /* Change our current working directory to that of the
643        crashing process. */
645     snprintf(cwd, PATH_MAX, "/proc/%s/cwd", argv[1]);
646     chdir(cwd);
648     /* Write output to file "core.info" in that directory. */
650     fp = fopen("core.info", "w+");
651     if (fp == NULL)
652         exit(EXIT_FAILURE);
654     /* Display command\-line arguments given to core_pattern
655        pipe program. */
657     fprintf(fp, "argc=%d\en", argc);
658     for (size_t j = 0; j < argc; j++)
659         fprintf(fp, "argc[%zu]=<%s>\en", j, argv[j]);
661     /* Count bytes in standard input (the core dump). */
663     tot = 0;
664     while ((nread = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
665         tot += nread;
666     fprintf(fp, "Total bytes in core dump: %zd\en", tot);
668     fclose(fp);
669     exit(EXIT_SUCCESS);
672 .SH SEE ALSO
673 .BR bash (1),
674 .BR coredumpctl (1),
675 .BR gdb (1),
676 .BR getrlimit (2),
677 .BR mmap (2),
678 .BR prctl (2),
679 .BR sigaction (2),
680 .BR elf (5),
681 .BR proc (5),
682 .BR pthreads (7),
683 .BR signal (7),
684 .BR systemd\-coredump (8)