1. Set the core file size to list the limits for all resources:
ulimit -a
Ulimit-a.png or view the core file size:
ulimit -c
Core File Size:
Unlimited:core file size is not restricted
0: The core file does not occur when the program fails
1024: Represents the 1024k,core file beyond this size cannot be generated
To set the core file size:
ulimit -c fileSize
Note:
Try to set the size of the file larger, and when the program crashes, the size of the core file is the amount of memory that the program consumes when it runs. Larger memory may occur when a stack overflow occurs
2. Set the name and file path of the core file
Default build Path: Enter executable file under the same path as Run command
Default build Name: Default name is core. The new core file overwrites the old core file
A. Setting the PID as the file name extension
1: Add PID as extension, the generated core file name is called Core.pid
0: Do not add pid as the extension, the resulting core file name is called core
Modified/proc/sys/kernel/core_uses_pid file content is: 1
Modify File command:echo "1" > /proc/sys/kernel/core_uses_pid
Or
sysctl -w kernel.core_uses_pid=1 kernel.core_uses_pid = 1
B. Controlling the core file save location and file name format
Modify File command:echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
Or:
sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t kernel.core_pattern = /corefile/core-%e-%p-%t
The core file can be generated uniformly into the/corefile directory, resulting in a file named core-command name-pid-timestamp
The following is a list of parameters:
%p-insert pid into filename add PID (Process ID)
%u-insert current UID to filename adds the present UID (user ID)
%g-insert current GID into filename to add an existing GID (user group ID)
%s-insert signal that caused the coredump into the filename adds a signal that causes the core to be generated
%t-insert Unix time, the coredump occurred into filename when you add a core file when you build Unix
%h-insert hostname where the coredump happened into filename adds host name
%e-insert coredumping executable name into filename adds the command name that causes the core to be generated
3. Test if you can generate a core file
kill -s SIGSEGV $$
See if a core file is generated under the/corefile directory
4. Debug the core file
Eg. TEST.c
#include<stdio.h>int main(){ int *p = NULL; *p = 0; return 0;}
[email protected]:~# gcc -o test test.c
[email protected]:~# ./test
Segmentation fault (core dumped)
Bingo: There is a segment error and a core file is generated
Found core-test-31421-1476266571 in the/corefile directory
Start debugging
gdb ./test core-test-31421-1476266571
Gdb
View the BU based on stack information
Linux build core file, core file path settings