1. Generate core file system configuration
Use the ulimit -c command to view the configuration of the core file in the current system. 0 means that no core file is generated; a number that is not 0 means that the size of the core file is limited to not more than a, and the unit is k; unlimited means that the size of the core file is not limited.
ulimit -c // 查看当前系统设置
ulimit -c 4096 // 修改系统设置,限制core文件大小不超过4096kb
ulimit -c unlimited // 修改系统设置,不限制core文件大小
The configuration of the test virtual machine is to limit the core file to no more than 1024kb:
2. Core file generation path configuration
Use the sysctl command or view the configuration file to obtain the current system core file generation path:
方法一:sysctl kernel.core_pattern
方法二:cat /proc/sys/kernel/core_pattern
How to modify the core file path:
(1) Execute the sysctl kernel.core_pattern=core path command to modify it, and it will fail after restarting
(2) Add kernel.core_pattern=core path in /etc/sysctl.conf; then execute sysctl -p to load
The core name itself can have the following information appended:
%p | Generate core file process ID |
%g | Generate the actual group ID of the core file |
%s | Generate core file signal |
%t | Generate core file timestamp |
%h | Generate core file hostname |
%e | Generate the core program file name |
%u | Generate the actual user ID of the core file |
The following uses temporary modification to modify the default path of the core file and add the time information of the core file:
3. Code testing
Save the following code as coretest.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
char *p = NULL;
*p = 1;
}
Execute the gcc -g coretest.c -o main command to generate an executable program, and directly execute ./main to run the executable program:
4. Core file debugging
Use the gdb command to select the core file and source program for debugging, and you can locate the error location of the program: