I. Relevant knowledge
1) What is the core file.
Core file, also known as Core dump, is a disk file that is written by the operating system when the process receives certain signals and terminates the runtime by writing out the contents of the process address space and other information about the state of the process. This information is often used for debugging. [Excerpt from Baidu Encyclopedia]
A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the progr AM has terminated abnormally (crashed). In practice, the other key pieces of the program state are usually dumped at the same time, including the processor registers, WHI CH may include the program counter and stack pointer, memory management information, and other processor and operating Sys TEM flags and information. The name comes from the Once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.
On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase ' to Dump core "has come to mean, at many cases, any fatal error, regardless of whether a record of the program memory is creat ed.[excerpt from wikipedia]
two. Example Ubuntu under the core file generation
1) To view the relevant environment, as shown in the following figure
Note: Core file size is 0,core file generation is not open
2 Write a simple problem with the C + + program and run
Note: Operation error, prompt segment error (Access to null pointer), because there is no switch to the core file, it does not produce core file information
3 Open the "switch" generated by the core file
Note: Set the core file size to open the production of core files, where 1024 is a preference value, can be adjusted according to the real value
4 Shipping program error and generate core files
Note: Core files are generated by default in the current working directory, and the file name is core
[Change the format of core file generation]
Note: The contents of the modified/proc/sys/kernel/core_pattern are core-%p-%e-%t, the original content is core
Proc/sys/kernel/core_pattern can control where the core file is saved and the file name format.
This file can be modified by the following command:
echo "Core-%e-%p-%t" >/proc/sys/kernel/core_pattern/core_pattern, the core file can be unified into the/corefile directory, The resulting file name is core-command name-pid-timestamp
The following is a list of parameters:
%p-insert pid into filename add PID
%u-insert present UID into filename add current UID
%g-insert present GID into filename add current GID
%s-insert signal that caused the coredump into the filename adds a signal leading to the core
%t-insert Unix time when the coredump occurred into filename to add a core file generation
%h-insert hostname where the coredump happened into filename add host name
%e-insert coredumping executable name into filename add command name
5 using GDB to debug core files
The
Core file View
core file needs to be viewed using GDB.
gdb./a.out
core-file core.xxxx
Use the BT command to see where the program went wrong. &NBSP
The following two command methods have the same effect, but are not effective in some environments, so the above command is recommended. &NBSP
1) gdb-core=core.xxxx
file./a.out
bt
2) gdb-c core.xxxx
file./a.out
BT