##core相关基本设置
- 使用ulimit -a 显示当前设置,可以做最简单的修改 ulimit -c 500(设置core文件大小最大为500KB)
- echo “1” > /proc/sys/kernel/core_uses_pid 为了生成的core文件带有PID后缀,便于后续分析。不要使用vi,因为 /proc/sys/kernel/下的文件为内存镜像,vi会出现Fsync failed错误。
##测试
- 编写程序 test_dump.c
#include <stdio.h>
int main(){
int *p = NULL;
*p = 1; //段错误,向空地址赋值
return 0;
}
- 编译
gcc -g test_dump.c -o test_dump_debug
-g:就是为了gdb调试
- 运行
root@ubuntu:/data/test/core_dump# ./test_dump_debug
Segmentationfault (core dumped)
在当前目录下生成了core.2030 文件,
补充:还要通过修改/proc/sys/kernel/core_pattern可以控制core文件保存位置和文件格式。
echo “/root/corefile/core-%e-%p-%t” > /proc/sys/kernel/core_pattern
- 调试
root@ubuntu:/data/test/core_dump# gdb test_dump_debug core.2030
就能看到
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fffacbfe000
Core was generated by’./test_dump_debug’.
Program terminated with signal 11, Segmentationfault.
#0 0x00000000004004c4 in main () at test_dump.c:4
4 *p = 1;
即test_dump.c文件的第4行就是crash的地方。