What is core dump
A core dump is a file of a computer’s documented memory of when a program or computer crashed. The file consists of the recorded status of the working memory at an explicit time, usually close to when the system crashed or when the program ended atypically. So a core dump file is very important for program developers since we often need it to locate and fix crash issues.
How to activate “core dump”
Firstly one can type
ulimit -c
in terminal console, if the output is 0,it means that the “core dump” is deactivated by default. In this case, Linux will not generate core dump file when a program crashes.Then one can activate “core dump” by setting a non-zero size for generated core dump file by executing
ulimit -c [kbytes]
. For example:
ulimit -c 100
:set maximum size of core dump file as 100kulimit -c unlimited
: no size limit of core dump file
Configuration of core dump
Once the core dump has been activated, we can get core dump file generated in local repository. There is still one issue: the core dump file has always the same name as “core” thus it is always over-written if multiple program crashes occur. What if we want to add pid in generated file name, or if we want this file is generated in some other directory?
- core dump filename with process id
1 | echo 1 > /proc/sys/kernel/core_uses_pid |
Then process id will be attached in file name.
- One can set the output path and coredump file name pattern by updating sysctl variable “kernel.core_pattern” in file “/etc/sysctl.conf”
Add the following phrase at the end of file sysctl.conf:
1 | kernel.core_pattern = /var/core/core_%e_%p |
Save and exit.
Below are the available options in core_pattern setting:
- %c maximum size of the generated file
- %e file name
- %g group ID of process
- %h host name
- %p process ID
- %s signal which triggered this coredump
- %t timestamp of this coredump (number of seconds since 1970-01-01)
- %u user ID of the dumped process
Once a modification has been made, one can execute the following command to make it take effect:
1 | sysctl –p /etc/sysctl.conf |
Please note: once we have set /proc/sys/kernel/core_uses_pid as 1,the generated core dump file still has pid in its name even we do not set %p in _core_pattern_.
Practice
In this section I present a small practice of coredump. Firstly I performed the following configurations (root privilege might be required) for coredump file generation:
1 | ulimit -c 1000000 |
Then let’s write some code to provoke a “Segmentation fault”. I put the code lines below into file “testSegmtFault.c “.
1 |
|
Compile it then execute the generated executable file:
1 | gcc -g testSegmtFault.c -o seg |
Execution output is as follows and there is a Segmentation Fault :D:
1 | qiu@qiu-ThinkCentre-E73:~/workspace/c_basics/linux_flux$ ./seg |
Now as expected, there is a coredump file named “core-seg-20017-1639863524” generated in /tmp/corefile/
Now let’s use gdb to debug the crash with help of the executable file and the coredump file:
1 | cd /tmp/corefile |
We can see that gdb immediately shows the defective code line number: