C programs under Linux often cause segment fault (segment error) due to memory access, and if the function of core dump is open, a file called core will be generated when running our executable program. We can then use GDB to tune the core file to restore the stack where the error occurred, which is very helpful for us to debug the bug.
1. Use ulimit-a to see the size limit of the current system core file;
Use Ulimit-c [Kbytes] To set the size of the core file that the system allows to generate.
Such as:
ULIMIT-C 0---Do not produce core files
ULIMIT-C---Set core file to 200k Max
ULIMIT-C Unlimited---Unlimited core file size
2. Let's write a procedure that will cause a section error.
The results of the compilation run are as follows:
Now look at our current file path and there is no core file generated.
3. Perform ulimit-a to view current system settings:
At this point we see that the current system core file size is 0, which means that no core files are generated.
Now we execute a command like this, without limiting the size of the generated core file.
Then we run our program and the visible core file is generated:
Finally, GDB is used to analyze the program and see the stack situation:
It is clear from the above output that the segment error appears on line 6th of testcore.c, and the problem is already fixed.
Many system default generated core file size is 0, like this to set the core file size, only valid in the current session, if the shutdown terminal restarts, then you need to enter the above command.
4. If you want to set the permanent effect, you can modify the/etc/security/limits.conf file, find * Soft core 0, modified to * soft core unlimited save, so every time you start to read this configuration file.
If you encounter the above situation, open limits.conf warning This is a read-only file, you can use the chmod command to modify the file permissions, add Write permissions.
Http://www.cnblogs.com/jiangson/p/5956674.html
How to generate a core dump file under Linux (fix segment fault segment error)