Explanation: When a segmentation error occurs, how does the Linux system generate the core file

  


The C program under Linux often causes a segment fault due to memory access errors, etc., if the system core dump function is Opened, then there will be a memory image dumped to the hard disk, then you can use gdb to analyze the core file, restore the stack situation when the system has a segmentation error. This is very helpful for us to find bugs.
Use ulimit -a to view the size limit of the system core file; use ulimit -c [kbytes] to set the size of the core file allowed to be generated by the system, for example
ulimit -c 0 does not generate core file
ulimit - c 100 Set the core file to a maximum of 100k
ulimit -c unlimited Do not limit the size of the core file
Look at a program that will cause a segmentation error:
#include
int main()
{ < Br> char *ptr="linuxers.cn";
*ptr=0;
}
The result of compiling and running is as follows:
[leconte@localhost test]$ gcc -g -o test Ac
[leconte@localhost test]$ ./test
Segment Error
There is no core file generated at this time, then use ulimit -c to set the core file size to unlimited, then execute the ./test 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
segment dump (core dumped)
[leconte@loc Alhost test]$ ls -al core.*
-rw------- 1 leconte leconte 139264 01-06 22:31 core.2065
Visible core file has been generated, then gdb analysis , see the stack situation:
[leconte@localhost test]$ gdb ./test core.2065
GNU gdb Fedora (6.8-27.el5)
Copyright (C) 2008 Free Software Foundation, Inc. < Br> 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" Br> 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 term Inated with signal 11, Segmentation fault.
[New process 2065]
#0 0x0804836f in main () at ac:6
6 *ptr=0;
It is clear from the above output The segmentation error occurred on line 6 of ac and the problem has been clearly located.
Many system default core file sizes are 0, we can specify the core file size by adding the ulimit -c command in the shell startup script /etc/bashrc or ~/.bashrc, etc., to ensure that the core file can generate.
In addition, you can also set the file name template of the core file in /proc/sys/kernel/core_pattern. For details, please refer to the official man page of the core.

Copyright © Windows knowledge All Rights Reserved