1. What is a core file?
A file with stack information and debug information generated when a "segmentation fault (core dumped)" occurred after the program in question was run.
When compiling, you need to add the -g option to make the program generate debugging information: gcc -g core_test.c -o core_test
2. How to configure and generate the core file
(1) core file switch
① Use ulimit -c to view the core switch, if it is 0, it means off, and no core file will be generated;
② Use ulimit -c [filesize] to set the core file size, and the core file will be generated when the minimum setting is 4;
③ Use ulimit -c unlimited to set the core file size to unlimited, which is a common practice;
④ If it needs to be executed at boot, you need to write this command to /etc/profile and other files.
(2) core file naming and saving path
①The core file has a default name and path, but for convenience, we usually name it ourselves and specify the save path;
② You can set the core file name and save path through /proc/sys/kernel/core_pattern , the method is as follows:
%p - insert pid into filename Add pid %u - insert current uid into filename Add current uid %g - insert current gid into filename Add current gid %s - insert signal that caused the coredump into the filename Add the signal that caused the core to be generated % t - insert UNIX time that the coredump occurred into filename Add the unix time when the core file was generated %h - insert hostname where the coredump happened into filename Add the hostnameecho "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
List of named arguments:
%e - insert coredumping executable name into filename Adds the command name .
3. Debug the core file
(1) Method 1: gdb [exec file] [core file] and execute bt to see the stack information:
(2) Method ②: gdb -c [core file] , then file [exec file] , and finally use bt to view the error location: