メインコンテンツにスキップ

coredump

Enable coredump

Usually, the core file will contain the memory, register status, stack pointer, memory management information, and various function call stack information when the program is running. We can understand it as the first file generated by the current state of the program. Many programs will generate a core file when they fail. By analyzing this file with tools, we can locate the corresponding stack call information when the program exits abnormally, find out the problem and solve it in time.

The system turns off coredump by default, which can be viewed through ulimit -c.

ulimit -c
0

If it is 0, it means that no core dump file is generated. If you want to enable coredump, you need to set the core file size and path.

Set the core file size

Use the command ulimit -c unlimited​ to set the size of the generated core file to unlimited, or you can set the size according to your needs.

The command is only valid for the current terminal. If you want to set it at system startup, you can edit the /etc/security/limits.conf file and add the following two lines:

* soft core unlimited
* hard core unlimited

Set the core file path

Set the path and name of the core file,

echo /var/crash/core-%e-%p-%t | sudo tee /proc/sys/kernel/core_pattern

The command is only valid for the current terminal. If you want to set it at system startup, you can edit the /etc/sysctl.conf file and add the following lines:

kernel.core_pattern = /var/crash/core-%e-%p-%t

Run the command sudo sysctl -p to make the configuration effective.

Test

Run the following command to test whether it works,

bash -c 'kill -SEGV $$'

If it works, you can see the following output,

Segmentation fault (core dumped)

At the same time, the corresponding core file will be generated in the /var/crash directory.