架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3693|回复: 0

使用 gdb 调试 php-fpm 异常错误

[复制链接]
发表于 2020-5-23 17:54:01 | 显示全部楼层 |阅读模式
GDB简介

GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。

问题

环境:Linux / PHP v5.5.7

在手机访问本站后台的时候,点击一些页面,会出现502错误,想找到具体是什么原因到导致的502错误。

php-fpm错误日志如下:

WARNING: [pool www] child 11274 exited on signal 11 (SIGSEGV) after 0.089068 seconds from start

core文件介绍

core文件其实就是内存的映像,当程序崩溃时,存储内存的相应信息,主用用于对程序进行调试。当程序崩溃时便会产生core文件,其实准确的应该说是core dump 文件,默认生成位置与可执行程序位于同一目录下,文件名为core.***,其中***是某一数字。

命令:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7271
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7271
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

core file size 为 0 ,则不能生成core文件,需要我们设置一下。

若ulimit -c unlimited,则表示core文件的大小不受限制
QQ截图20200523164952.jpg

设置完成,记得重启下php-fpm服务,命令如下:



再次查看错误日志,如下:

[23-May-2020 14:39:14] WARNING: [pool www] child 2220 exited on signal 11 (SIGSEGV - core dumped) after 7.255225 seconds from start
[23-May-2020 14:39:14] NOTICE: [pool www] child 2231 started

日志中含有“SIGSEGV – core dumped”字样,就代表生成rore文件成功。

core文件会生成在网站目录下面,如果不知道的话,也可以通过如下命令查找core文件:


我服务器core生成的地址为:/alidata/www/itsvse_web/core.2220

安装gdb

命令如下:



gdb 调试 php-fpm

准备 .gdbinit 文件

.gdbinit 文件在PHP源代码下面,一定要和运行的php版本一致!

我的文件地址:/root/sh-1.5.5/php-5.5.7/.gdbinit

最新php版本的.gdbinit文件下载地址:

https://github.com/php/php-src/blob/master/.gdbinit

gdb 打开 core 文件

在core文件目录下,执行如下命令:

[root@o itsvse_web]# gdb php-fpm -c core.2220
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.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 /alidata/server/php-5.5.7/sbin/php-fpm...done.
[New LWP 2220]
[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  tsrm_realpath_r (
    path=path@entry=0x7ffdd9ebf100 "/alidata/www/itsvse_web/./source/language/mobile/lang_template.php",
    start=start@entry=1, len=66, ll=ll@entry=0x7ffdd9ebf0f4, t=t@entry=0x7ffdd9ebf0f8,
    use_realpath=use_realpath@entry=2, is_dir=is_dir@entry=0, link_is_dir=link_is_dir@entry=0x0)
    at /root/sh-1.5.5/php-5.5.7/TSRM/tsrm_virtual_cwd.c:751
751        {
Missing separate debuginfos, use: debuginfo-install cyrus-sasl-lib-2.1.26-23.el7.x86_64 glibc-2.17-307.el7.1.x86_64 keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.15.1-46.el7.x86_64 libcom_err-1.42.9-17.el7.x86_64 libcurl-7.29.0-57.el7.x86_64 libgcc-4.8.5-39.el7.x86_64 libidn-1.28-4.el7.x86_64 libselinux-2.5-15.el7.x86_64 libssh2-1.8.0-3.el7.x86_64 libstdc++-4.8.5-39.el7.x86_64 libxml2-2.9.1-6.el7.4.x86_64 nspr-4.21.0-1.el7.x86_64 nss-3.44.0-7.el7_7.x86_64 nss-softokn-freebl-3.44.0-8.el7_7.x86_64 nss-util-3.44.0-4.el7_7.x86_64 openldap-2.4.44-21.el7_6.x86_64 openssl-libs-1.0.2k-19.el7.x86_64 pcre-8.32-17.el7.x86_64 xz-libs-5.2.2-1.el7.x86_64 zlib-1.2.7-18.el7.x86_64

可以看到类似下边的字样:

Core was generated by `php-fpm: pool www            '.
Program terminated with signal 11, Segmentation fault.

bt(backtrace):列出调用栈

(gdb) bt
#0  tsrm_realpath_r (
    path=path@entry=0x7ffdd9ebf100 "/alidata/www/itsvse_web/./source/language/mobile/lang_template.php",
    start=start@entry=1, len=66, ll=ll@entry=0x7ffdd9ebf0f4, t=t@entry=0x7ffdd9ebf0f8,
    use_realpath=use_realpath@entry=2, is_dir=is_dir@entry=0, link_is_dir=link_is_dir@entry=0x0)
    at /root/sh-1.5.5/php-5.5.7/TSRM/tsrm_virtual_cwd.c:751
#1  0x000000000075209a in virtual_file_ex (state=state@entry=0x7ffdd9ec0140,
    path=path@entry=0xed3a4d8 "/alidata/www/itsvse_web/./source/language/mobile/lang_template.php",
    verify_path=verify_path@entry=0x0, use_realpath=use_realpath@entry=2)
    at /root/sh-1.5.5/php-5.5.7/TSRM/tsrm_virtual_cwd.c:1292
#2  0x000000000075308c in tsrm_realpath (
    path=path@entry=0xed3a4d8 "/alidata/www/itsvse_web/./source/language/mobile/lang_template.php",
    real_path=real_path@entry=0x7ffdd9ec1250 "vse_web/./source/plugin/dsu_amupper/pper.class.p\300\023\354\331\375\177") at /root/sh-1.5.5/php-5.5.7/TSRM/tsrm_virtual_cwd.c:1954

QQ截图20200523173930.jpg

查看退出线程


引入 PHP 源代码中提供的 .gdbinit (gdb 命令编写脚本)



查看 zbacktrace 和变量值

(gdb) zbacktrace
[0xed9f8f0] lang() /alidata/www/itsvse_web/source/function/function_core.php:444
[0xed9e2e8] lang("core", "title_board_message") /alidata/www/itsvse_web/source/function/function_message.php:43
[0xed9a988] dshowmessage("mobile_template_no_found", "", array(1)[0xec46a28], array(0)[0xec46bc0], 0) /alidata/www/itsvse_web/source/function/function_core.php:1426
[0xed9a6d8] showmessage("mobile_template_no_found", "", array(1)[0xec46a28]) /alidata/www/itsvse_web/source/function/function_core.php:618
[0xed97638] template("dsu_amupper:pper_foot") /alidata/www/itsvse_web/source/plugin/dsu_amupper/pper.class.php:82
[0x7ffdd9ec3e50] plugin_dsu_amupper->global_footer(array(0)[0xec40028])
[0xed97080] call_user_func(array(2)[0xed6b8b0], array(0)[0xec40028]) /alidata/www/itsvse_web/source/function/function_core.php:1177
[0xed954b8] hookscript("global", "global") /alidata/www/itsvse_web/source/function/function_core.php:1214
[0xed95218] hookscriptoutput("showmessage") /alidata/www/itsvse_web/data/template/8_8_touch_common_showmessage.tpl.php:1
[0xed949e8] ??? /alidata/www/itsvse_web/source/function/function_message.php:237
[0xed91088] dshowmessage("mobile_template_no_found", "", array(1)[0xec40c10], array(0)[0xec76d40], 0) /alidata/www/itsvse_web/source/function/function_core.php:1426
[0xed90dd8] showmessage("mobile_template_no_found", "", array(1)[0xec40c10]) /alidata/www/itsvse_web/source/function/function_core.php:618
[0xed8dd38] template("dsu_amupper:pper_foot") /alidata/www/itsvse_web/source/plugin/dsu_amupper/pper.class.php:82
[0x7ffdd9ec49a0] plugin_dsu_amupper->global_footer(array(0)[0xec39ab0])
[0xed8d780] call_user_func(array(2)[0xeb833e8], array(0)[0xec39ab0]) /alidata/www/itsvse_web/source/function/function_core.php:1177
[0xed8bbb8] hookscript("global", "global") /alidata/www/itsvse_web/source/function/function_core.php:1214
[0xed8b918] hookscriptoutput("showmessage") /alidata/www/itsvse_web/data/template/8_8_touch_common_showmessage.tpl.php:1
[0xed8b0e8] ??? /alidata/www/itsvse_web/source/function/function_message.php:237
[0xed651b0] dshowmessage("mobile_template_no_found", "", array(1)[0xecc0fe0], array(0)[0xec76718], 0) /alidata/www/itsvse_web/source/function/function_core.php:1426
[0xed64f00] showmessage("mobile_template_no_found", "", array(1)[0xecc0fe0]) /alidata/www/itsvse_web/source/function/function_core.php:618
[0xed61e60] template("dsu_amupper:pper_foot") /alidata/www/itsvse_web/source/plugin/dsu_amupper/pper.class.php:82
[0x7ffdd9ec54f0] plugin_dsu_amupper->global_footer(array(0)[0xeb834f8])
QQ截图20200523174920.jpg
(gdb) print ((zval *)0xec46a28)
$1 = (zval *) 0xec46a28
(gdb) printzv $1
[0xec46a28] (refcount=5) array(1): {
    "url\0" => [0xecb2fb0] (refcount=2) string(33): "/admin.php?action=index&mobile=no"
  }
(gdb)
解决办法

我其实也没看出来啥错误,但是从日志明显看到手机访问出了问题,经过测试,确实只有在手机访问的情况下出现502错误。

因为后台,根本就没有手机版本,我直接修改 admin.php 源码,第一行增加如下代码:

这样程序永远得到的是来自pc浏览器的访问请求了,就不会走手机版本的逻辑代码,解决!




上一篇:Nginx 自定义404错误页面,返回200状态码解决方案
下一篇:Angular 中[ngClass]、[ngStyle]的基本使用
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-23 18:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表