Reprint: http://leonzhan.iteye.com/blog/803008
Linux under the C program often because of memory access errors such as Segment fault (segment error), at this point if the system core dump function is open, then there will be memory image dump to the hard disk, then you can use GDB on the core file analysis, Restores the stack condition at the time of the system segment error. This is helpful for us to discover bugs in the program.
Use Ulimit-a to view the size limit of the core file of the system; use Ulimit-c [Kbytes] To set the core file size that the system allows to generate, for example [plain] view Plain copy ulimit-c 0 does not produce Core file Ulimit-c 100 set core file Max to 100k Ulimit-c Unlimited does not limit core file size
Take a look at a program that causes a mistake:
[CPP] view plain copy #include <stdio.h> int main () {char *ptr= "linuxers.cn"; *ptr=0; }
The results of the compile run are as follows: [plain] view plain copy [Leconte@localhost test]$ gcc-g-o test a.c [leconte@localhost test]$. T EST segment Error
At this point, the core file is not generated, then use ULIMIT-C to set the core file size to No limit, and then execute./test program, the results are as follows:
[plain] view plain copy [leconte@localhost ~]$ ulimit-a core file size (blocks,-c) 0 ... [Leconte@localhost test]$ Ulimit-c Unlimited [leconte@localhost test]$ the core file size (ulimit-a,-C ) unlimited ........ [Leconte@localhost test]$/test segment Error (core dumped) [Leconte@localhost test]$ ls-al core.*-rw-------1 Leconte Leco Nte 139264 01-06 22:31 core.2065
Visible core files have been generated, and then you can use GDB analysis to view the stack:
[Plain] View Plain copy [leconte@localhost test]$ gdb ./test core.2065 GNU gdb Fedora (6.8-27.EL5) copyright (C) 2008 Free Software foundation, inc. License gplv3+: gnu gpl version 3 or later
It is clear from the above output that the segment error appears in the 6th line of the A.C, and the problem has been clearly positioned.
Many of the system default core file sizes are 0, and we can specify the core file size by adding the ulimit-c command to the shell's startup script/ETC/BASHRC or ~/.BASHRC, to ensure that the core file is generated.
In addition, you can also set the filename template for the core file in/proc/sys/kernel/core_pattern, see the Official Man Handbook for details.