一、错误文件生成
Linux下C/C++程序或nginx等出现错误
1、错误程序
10 / 0产生错误
#include <stdio.h>
int func(int a, int b)
{
return a / b;
}
int main()
{
int x = 10;
int y = 0;
printf("%d / %d = %d\n", x, y, func(x, y));
return 0;
}
2、编译错误程序
gcc -g -o test1 test1.c
3、执行错误程序
./test1
4、查看dmesg日志
[2284281.015315] traps: test1[2782294] trap divide error ip:55b47b4f915b sp:7ffd4fd07070 error:0 in test1[55b47b4f9000+1000]
二、addr2line
使用addr2line分析错误点
addr2line 55b47b4f915b -e test1
??:0尚未解决
三、通过core文件解决
1、打开core文件配置
开启写core文件且不限制大小
ulimit -c unlimited
2、配置core文件写入地址
root@VM-4-2-ubuntu:/home/lighthouse/test# cat /proc/sys/kernel/core_pattern
core
也可以配置到指定位置,如:
echo "/corefile/core-%e-%p-%t" > core_pattern
3、nginx打开写core文件及位置
nginx配置文件nginx.conf配置,并reload nginx
worker_rlimit_core 10000m;
working_directory /usr/local/nginx/logs;
4、执行程序生成core文件
root@VM-4-2-ubuntu:/home/lighthouse/test# ./test1
Floating point exception (core dumped)
5、gdb分析core文件
gdb test1 core
bt打印堆栈信息
l查看源码
b行号
更多GDB调试命令:
linux下gdb调试方法与技巧整理