No relevant resource is found in the selected language.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read our privacy policy>Search

Reminder

To have a better experience, please upgrade your IE browser.

upgrade

Core File Are Generated When a Segment Error Occurs in Linux

Publication Date:  2019-04-23 Views:  39 Downloads:  0
Issue Description

Core files are generated when a segment error occurs in Linux.

Solution

In Linux, the C program may encounter a segment error due to memory access errors. If the core dump function is enabled, the memory image is dumped to the hard disk. In this case, you can use the gdb to analyze the core file to restore the system stack information at the time when the segment error occurs. This is helpful for finding program bugs. 
    To check the size limit of core files, run ulimit -a. To set the maximum size of core files that can be generated by the system, run ulimit -c [kbytes]. For example, ulimit -c 0 does not generate core files; ulimit -c 100 set the maximum size of core files to 100k; ulimit -c unlimited does not limit the size of core files.
The following is a program that may generate a fragment error:
    #include
    int main()
    {
    char *ptr="linuxers.cn";
    *ptr=0;
    }
After the program is compiled and executed, the result is as follows:
    [leconte@localhost test]$ gcc -g -o test a.c
    [leconte@localhost test]$ ./test
....fragment error
If no core file is generated, run ulimit -c to unlimit the size of the core files, and run the ./test to test the program. The result is as follows:
    [leconte@localhost ~]$ ulimit -a
    core file size (blocks, -c) 0
    [leconte@localhost test]$ ulimit -c unlimited
    [leconte@localhost test]$ ulimit -a
    core file size (blocks, -c) unlimited
    [leconte@localhost test]$ ./test
....fragment error (core dumped)
    [leconte@localhost test]$ ls -al core.*
    -rw------- 1 leconte leconte 139264 01-06 22:31 core.2065
The result shows that a core file is generated. You can use gdb to analyze the system stack information.
    [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
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law. Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...
    warning: exec file is newer than core file.
    warning: Can't read pathname for load map: Input/output error.
    Reading symbols from /lib/libc.so.6...done.
    Loaded symbols for /lib/libc.so.6
    Reading symbols from /lib/ld-linux.so.2...done.
    Loaded symbols for /lib/ld-linux.so.2
    Core was generated by `./test'.
    Program terminated with signal 11, Segmentation fault.
    [New process 2065]
    #0 0x0804836f in main () at a.c:6
    6 *ptr=0;
The preceding output clearly shows that the segment error occurs in line 6 of a.c .
    In many systems, the default size of core files is 0. You can add ulimit -c to the shell startup script /etc/bashrc or ~/.bashrc to specify the core file size to ensure that core files can be correctly generated.
    In addition, you can set the file name template of the core file in /proc/sys/kernel/core_pattern. For details, see the system manual.

END