Sky23

how gdb nginx core

This project is maintained by wangfakang

如何去追nginx的core dump:

我们在追一个程序挂掉的时候的方法:

1.一般都会去找core文件,要是没有在输入可执行文件运行命令的同一路径下找到该core文件?

那么首先就看:
ulimit -c 如果是0的话,那就是你没有打开core dump。就不会产生了。 如果发现不是0的话,而且在当前路径下还找不到coredump文件,对了如果你不知道当前程序 启动时候的路径的话可以 sudo ls /proc/pid/cwd 然后找到。

如果没有在当前启动程序的路径下找到core dump文件,那么就执行: sudo sysctl -a | grep "core_pattern" 然后看看把core dump产生的路径设置到那个下面了。 当然你自己也可以设置:

可以使用: echo "1" > /proc/sys/kernel/core_uses_pid 让你的core后缀是pid
echo "/corefile/core-%e-%p-%t" > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
    %p - insert pid into filename 添加pid
    %u - insert current uid into filename 添加当前uid
    %g - insert current gid into filename 添加当前gid
    %s - insert signal that caused the coredump into the filename 添加导致产生core的信号
    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
    %h - insert hostname where the coredump happened into filename 添加主机名
    %e - insert coredumping executable name into filename 添加命令名

最后一步就来gdp binary corefile [binary是可执行程序名称,corefile是coredump名字] 执行bt打印函数的堆栈信息,定位到那个函数的问题,然后细看code。

2.前面说的都是有core dump文件产生了,那要是没有core 产生,但是通过dmesg又看到类似如下日志:

[506319.457950] nginx[18122]: segfault at 0 ip 00007fe9b3336ae sp 00007aaf3aa9523 error 5 in libc-2.12.so[7ff9b327a000+18a000]

这个时候可以使用如下这两个神器来查是那个函数出问题了:

一.通过反汇编的方式来查看:

  1. gdb binary

  2. disassemble 00007fe9b3336ae (其中00007fe9b3336ae就是上面字段ip后面的值)

二.使用addr2line工具:

1.addr2line -e binary 00007fe9b3336ae -f (其中00007fe9b3336ae就是上面字段ip后面的值,binary是可执行文件) 可以找到出问题的文件名和行号.

Communite

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

Thx

Author