Preface
Recently encountered large projectsProgramWhen SIGSEGV appeared, I never knew to use the core dump tool to debug the program. It took nearly a week to locate the problem. The boss was angry and had serious consequences, I learned this knowledge carefully afterwards and learned a little about core dump.
Before using semiconductors as memory materials, humans use coils as memory materials (inventor Wang An), and coils are called core, and the memory made of coils is called core memory ". (The coil word should be coil, huh, huh) today, the semiconductor industry is booming and no one uses coils as memory. However, in many cases, people still call Memory "core ". So Note: here the core is not the core, but the memory. However, based on the actual situation, it seems that the kernel occupies memory.
What is core dump? When developing (or using) a program, what we fear most is that the program is suddenly suspended. Although the system is okay, we may encounter the same problem next time. Therefore, the operating system will write the memory content when the program goes down to a file called core (This write action is called dump. Dump means garbage and dumping. From this point of view, the content of these memories is the result of program error running, so it is garbage. Getting it out is like "dumping" from a large memory pool ".), For debugging. This process is called core dump. in an embedded system, sometimes the core dump is printed directly from the serial port, combined with the objdump to find the RA and EPA addresses, and stack backtracking can be used to find the places where the program fails.
How to generate a core dump file
In general Linux, core dump files are not generated by default. You can use ulimit-C to check the size of the core dump file. Generally, the starting value is 0. You can set the core file size, ulimit-C 1024 (Kbytes unit) or ulimit-C unlimited.
Core dump file output settings
The default directory is the current directory. You can find it in/proc/sys/kernel.To core-user-pid
1. Add the PID number to the Core File Name
Echo"1">/Proc/sys/kernel/Core-user-PID
2. Control the core file storage location and file name format
Mkdir-P/root/corefile
Echo"/Root/corefile/Core-% E-% P-% t">/Proc/sys/kernel/Core-Pattern
The following is a list of parameters:
% 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 added to generate core Signal
% T-insert UNIX time that the coredump occurred into filename UNIX time when the core file is generated
% H-insert hostname where the coredump happened into filename Add the Host Name
% E-insert coredumping executable name into filename add command name
Use GDB to view core files
In the following example, the core dump occurs when a message number error occurs. during compilation, the-G
After a core dump occurs, use GDB to view the content of the core file to locate the line that causes the core dump in the file.
GDB [exec file] [core file]
For example:
GDB./test. Core
After entering GDB, run the BT command to check where the program is running and locate the file line of core dump.
Instance description
//Test. c
VoidA ()
{
Char* P = NULL;
Printf ("% D/n", * P );
}
IntMain ()
{
A ();
Return 0;
}
Compile
Gcc-g-o Test test. c
Run./test
Segmentation fault (core dump)
If test. Core.
GDB./test. Core