使用phpbrew编译的PHP8.1.9运行脚本触发Segmentation fault调试求助
使用phpbrew编译的自定义PHP 8.1.9版本,运行Magento的bin/magento command:feed:generate default脚本时触发Segmentation fault(段错误)。已通过gdb调试,相关输出如下:
gdb启动及运行输出
/var/www/vhosts/example.com/httpdocs$ gdb -args /opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php -c .user.ini -dmemory_limit=4G bin/magento command:feed:generate default GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2 Copyright (C) 2020 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-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from /opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php... (gdb) run Starting program: /opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php -c .user.ini -dmemory_limit=4G bin/magento command:feed:generate default [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Xdebug: [Config] The setting 'xdebug.remote_enable' has been renamed, see the upgrading guide at https://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_enable (See: https://xdebug.org/docs/errors#CFG-C-CHANGED) Xdebug: [Config] The setting 'xdebug.remote_host' has been renamed, see the upgrading guide at https://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_host (See: https://xdebug.org/docs/errors#CFG-C-CHANGED) Xdebug: [Config] The setting 'xdebug.remote_port' has been renamed, see the upgrading guide at https://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_port (See: https://xdebug.org/docs/errors#CFG-C-CHANGED) Lock Acquired Processing... Fetchings product in included categories Fetching products in excluded categories Fetching products data Program received signal SIGSEGV, Segmentation fault. 0x00007ffff737dfdb in __strstr_sse2 (haystack=0x7fffeda2c8a0 "Vendor\\Module\\Helper\\Data", needle=0x7ffff4692b39 "@anonymous") at ../string/strstr.c:84 84 ../string/strstr.c: No such file or directory.
where命令输出
#0 0x00007ffff737dfdb in __strstr_sse2 (haystack=0x7fffeea2de88 "Vendor\\Module\\Helper\\Data", needle=0x7ffff4692b39 "@anonymous") at ../string/strstr.c:84 #1 0x00007ffff466438e in xdebug_build_fname (tmp=tmp@entry=0x7fffe9db2b20, edata=edata@entry=0x7fffea46f120) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:218 #2 0x00007ffff46647ca in xdebug_add_stack_frame (zdata=<optimized out>, zdata@entry=0x7fffea46f070, op_array=op_array@entry=0x7fffeb955ae0, type=type@entry=1) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:615 #3 0x00007ffff4664f88 in xdebug_execute_ex (execute_data=0x7fffea46f120) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:721 #4 0x0000555555b2f24d in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:1837 #5 execute_ex (ex=0x7fffeea2de88) at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:55788 #6 0x00007ffff4665092 in xdebug_execute_ex (execute_data=0x7fffea46f070) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:779 #7 0x0000555555b2f24d in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:1837 #8 execute_ex (ex=0x7fffeea2de88) at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:55788 #9 0x00007ffff4665092 in xdebug_execute_ex (execute_data=0x7fffea46efc0) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:779 #10 0x0000555555b2f24d in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:1837 #11 execute_ex (ex=0x7fffeea2de88) at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:55788 #12 0x00007ffff4665092 in xdebug_execute_ex (execute_data=0x7fffea46ef10) at /opt/phpbrew/build/phpbrew8_1_9_fdev/ext/xdebug/src/base/base.c:779 #13 0x0000555555b2f24d in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:1837 #14 execute_ex (ex=0x7fffeea2de88) at /opt/phpbrew/build/phpbrew8_1_9_fdev/Zend/zend_vm_execute.h:55788 #15 0x00007ffff4665092 in xdebug_execute_ex
执行c(continue)命令后,上述错误栈会无限重复,持续数分钟。
临时禁用Xdebug验证兼容性
运行脚本时通过参数临时关闭Xdebug,确认是否为Xdebug导致的问题:/opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php -c .user.ini -dmemory_limit=4G -dzend_extension=xdebug.so=0 bin/magento command:feed:generate default若脚本正常执行,说明Xdebug与当前PHP版本或业务代码存在兼容问题,可尝试升级Xdebug到适配PHP 8.1的稳定版,或先修正Xdebug的废弃配置警告。
深入分析gdb栈帧细节
触发段错误后不要直接continue,逐个查看栈帧信息定位问题:- 用
frame n切换到指定栈帧(如frame 1进入xdebug_build_fname函数栈帧) - 用
print命令查看关键变量(如print tmp、print edata),检查是否存在非法内存访问 - 用
list命令查看当前栈帧对应的代码上下文(若有源码文件)
- 用
检查PHP编译参数
通过php -i | grep configure查看phpbrew编译PHP的参数,对比Magento官方推荐的兼容编译配置,确认是否开启了不兼容选项或缺少依赖。排查业务代码问题
错误栈指向Vendor\Module\Helper\Data,结合@anonymous关键词,检查该Helper类中匿名类、闭包相关代码,是否存在内存泄漏或非法引用。可通过注释部分代码逐步定位触发段错误的具体逻辑。生成Core Dump做深度分析
开启core dump功能生成完整内存镜像,便于后续排查:ulimit -c unlimited /opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php -c .user.ini -dmemory_limit=4G bin/magento command:feed:generate default生成core文件后,用
gdb /opt/phpbrew/php/phpbrew8_1_9_fdev/bin/php core.xxx加载,执行bt full查看完整栈信息与变量值。
内容的提问来源于stack exchange,提问作者gabtzi

