PHP-FPM如何產生core dump檔案

這陣子裝了Jetpack plugin

發現一直有 EXITED ON SIGNAL 11 (SIGSEGV) 的問題

改了一堆設定

把buffer加大

timeout加大

都沒用

伺服器上的php是用remi repo安裝的

預設沒有開啟debug

所以無法產生core dump文件

今天心一橫

決定自己來compile php

首先為了達成差不多的環境

php版本用了一樣的7.1.3

comfigure option是用php-config來看remi repo的configure option

果然是–disable-debug

把這參數改成 –enable-debug其它照舊

步驟也很簡單

# ./configure XXXXX --enable-debug
# make
# make install

中間會遇到一些程式庫沒裝

基本上只要用 yum search尋找相關函式庫

再yum install就能解決99%的問題

如果再不行就Google吧!

或是我乾脆有拿掉一些用不到的extension

像是OCI8我用不到就直接拿掉不compile

還有安裝的路徑原本是

/opt/remi/php71/root/usr

為了不要衝突我改成

/opt/remi/php71-debug/root/usr

因為–enable-debug所有extension都要重新compile

不能共用的!

Compile完之後複製原本的php-fpm.conf跟www.conf

修改php-fpm.conf

include=/etc/opt/remi/php71-debug/php-fpm.d/*.conf

修改www.conf

rlimit_core = unlimited

之後就停止原本的php-fpm service

# systemctl stop php71-fpm

在啟動之前還有一個步驟

# echo "/tmp/core.%p" > /proc/sys/kernel/core_pattern
# ulimit -c unlimited

%p是pid的意思

還有多個參數可加進檔名裡

%p: pid
%: '%' is dropped
%%: output one '%'
%u: uid
%g: gid
%s: signal number
%t: UNIX time of dump
%h: hostname
%e: executable filename
%: both are dropped

現在就可以手動啟動debug版本的php-fpm

# /opt/remi/php71-debug/root/usr/sbin/php-fpm

想辦法讓crash重現

我只需要重新安裝Jetpack plugin就會發生錯誤

這次的錯誤訊息變成了

[08-Apr-2017 21:43:27] WARNING: [pool www] child 1974 exited on signal 11 (SIGSEGV - core dumped) after 297.672834 seconds from start

多了 core dumped

然後在/tmp下可以發現一個檔案

我的是 core.1974

1974就是錯誤訊息裡crash的process pid

之後用gdb開啟

# gdb /opt/remi/php71-debug/root/usr/sbin/php-fpm /tmp/core.1974

就可以看到如下的訊息

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /opt/remi/php71-debug/root/usr/sbin/php-fpm...done.
[New LWP 1974]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `php-fpm: pool www                          '.
Program terminated with signal 11, Segmentation fault.
#0  0x00007f027b1adce8 in __memcpy_ssse3_back () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install bzip2-libs-1.0.6-13.el7.x86_64 cyrus-sasl-lib-2.1.26-20.el7_2.x86_64 freetype-2.4.11-12.el7.x86_64 glibc-2.17-157.el7_3.1.x86_64 keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.14.1-27.el7_3.x86_64 libX11-1.6.3-3.el7.x86_64 libXau-1.0.8-2.1.el7.x86_64 libXpm-3.5.11-3.el7.x86_64 libcom_err-1.42.9-9.el7.x86_64 libcurl-7.29.0-35.el7.centos.x86_64 libedit-3.0-12.20121213cvs.el7.x86_64 libgcc-4.8.5-11.el7.x86_64 libgcrypt-1.5.3-13.el7_3.1.x86_64 libgpg-error-1.12-3.el7.x86_64 libicu-50.1.2-15.el7.x86_64 libidn-1.28-4.el7.x86_64 libjpeg-turbo-1.2.90-5.el7.x86_64 libmcrypt-2.5.8-13.el7.x86_64 libpng-1.5.13-7.el7_2.x86_64 libselinux-2.5-6.el7.x86_64 libssh2-1.4.3-10.el7_2.1.x86_64 libstdc++-4.8.5-11.el7.x86_64 libtidy-0.99.0-31.20091203.el7.x86_64 libtool-ltdl-2.4.2-21.el7_2.x86_64 libxcb-1.11-4.el7.x86_64 libxml2-2.9.1-6.el7_2.3.x86_64 libxslt-1.1.28-5.el7.x86_64 ncurses-libs-5.9-13.20130511.el7.x86_64 nspr-4.13.1-1.0.el7_3.x86_64 nss-3.28.2-1.6.el7_3.x86_64 nss-softokn-3.16.2.3-14.4.el7.x86_64 nss-softokn-freebl-3.16.2.3-14.4.el7.x86_64 nss-sysinit-3.28.2-1.6.el7_3.x86_64 nss-util-3.28.2-1.1.el7_3.x86_64 openldap-2.4.40-13.el7.x86_64 openssl-libs-1.0.1e-60.el7_3.1.x86_64 pcre-8.32-15.el7_2.1.x86_64 sqlite-3.7.17-8.el7.x86_64 xz-libs-5.2.2-1.el7.x86_64 zlib-1.2.7-17.el7.x86_64

在這邊就可以看到哪邊的問題

如要更多資訊可以用bt指令

(gdb) bt
#0  0x00007f027b1adce8 in __memcpy_ssse3_back () from /lib64/libc.so.6
#1  0x00000000006d4884 in zend_hash_packed_to_hash (ht=0x7f025f8a4480) at /root/php-7.1.3/Zend/zend_hash.c:214
#2  0x00000000006d5b05 in _zend_hash_add_or_update_i (ht=0x7f025f8a4480, key=0x7f0260e24968, pData=0x7f026397bb60, flag=1,
    __zend_filename=0xbd4100 "/root/php-7.1.3/Zend/zend_vm_execute.h", __zend_lineno=5886) at /root/php-7.1.3/Zend/zend_hash.c:554
#3  0x00000000006d5ebb in _zend_hash_update (ht=0x7f025f8a4480, key=0x7f0260e24968, pData=0x7f026397bb60,
    __zend_filename=0xbd4100 "/root/php-7.1.3/Zend/zend_vm_execute.h", __zend_lineno=5886) at /root/php-7.1.3/Zend/zend_hash.c:627
#4  0x000000000072dbeb in ZEND_ADD_ARRAY_ELEMENT_SPEC_CONST_CONST_HANDLER () at /root/php-7.1.3/Zend/zend_vm_execute.h:5886
#5  0x0000000000721c72 in execute_ex (ex=0x7f0279c14030) at /root/php-7.1.3/Zend/zend_vm_execute.h:429
#6  0x0000000000721d84 in zend_execute (op_array=0x7f0279c77000, return_value=0x0) at /root/php-7.1.3/Zend/zend_vm_execute.h:474
#7  0x00000000006c353d in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /root/php-7.1.3/Zend/zend.c:1476
#8  0x00000000006331b3 in php_execute_script (primary_file=0x7ffd9f159140) at /root/php-7.1.3/main/main.c:2537
#9  0x00000000007b1d2d in main (argc=1, argv=0x7ffd9f159368) at /root/php-7.1.3/sapi/fpm/fpm/fpm_main.c:1966

我這看起來像是核心的hash出了問題

所以我提交給php.net處理了

看更多