How to generate core dump files under Linux __linux

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.