php运行出现Segmentation fault

发现 Segmentation fault

今天安装了 xdebug、xhrpof等分析工具, 然后在运行相关命令时出现了 Segmentation fault(分段错误), 一般出现此错误大部分 内存越界指针错误引起的原因, 并非 php 代码本身原因. 出现此问题大部分因为底层拓展导致。

示例:

php artisan xxx:xxxx

output:
Segmentation fault

生成core dump

这种错误信息, 借助 core.dump 文件分析是种比较好的办法

默认文件是关闭的, 开启生成 core.dump 文件

ulimit -c unlimited

重新执行命令

php artisan xxx:xxxx

Segmentation fault (core dumped)

这时默认就在当前目录下生成了 core 文件, 我们借助 gdb 来看下

调试

yum install gdb

执行 gdb , 直接 bt 命令查看堆栈信息

gdb php core
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00000000008f8eb9 in ZEND_USER_OPCODE_SPEC_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1806
#2  0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#3  0x00000000008f44bf in ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:35533
#4  0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#5  0x00000000008f7a35 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:949
#6  0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#7  0x0000000000852981 in zend_call_function (fci=fci@entry=0x7ffee921ebc0, fci_cache=fci_cache@entry=0x7ffee921eb90) at /usr/local/src/php-7.1.9/Zend/zend_execute_API.c:855
#8  0x000000000087e7b0 in zend_call_method (object=0x7f205d2720c8, obj_ce=0x7f205d203600, fn_proxy=<optimized out>, 
    function_name=0x7f205d276018 "composer\\autoload\\classloader::loadclass\001", function_name_len=<optimized out>, retval_ptr=retval_ptr@entry=0x0, 
    param_count=param_count@entry=1, arg1=0x7f205d213a80, arg2=arg2@entry=0x0) at /usr/local/src/php-7.1.9/Zend/zend_interfaces.c:99
#9  0x00000000007287e1 in zif_spl_autoload_call (execute_data=<optimized out>, return_value=<optimized out>) at /usr/local/src/php-7.1.9/ext/spl/php_spl.c:420
#10 0x0000000000852ad9 in zend_call_function (fci=fci@entry=0x7ffee921ee20, fci_cache=fci_cache@entry=0x7ffee921edf0) at /usr/local/src/php-7.1.9/Zend/zend_execute_API.c:871
#11 0x0000000000852f76 in zend_lookup_class_ex (name=name@entry=0x7f204b179c70, key=0x7f204c538a80, use_autoload=use_autoload@entry=1)
    at /usr/local/src/php-7.1.9/Zend/zend_execute_API.c:1028
#12 0x0000000000853888 in zend_fetch_class_by_name (class_name=0x7f204b179c70, key=<optimized out>, fetch_type=fetch_type@entry=512)
    at /usr/local/src/php-7.1.9/Zend/zend_execute_API.c:1463
#13 0x00000000008f2c8f in ZEND_INIT_STATIC_METHOD_CALL_SPEC_CONST_CONST_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:5479
#14 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#15 0x0000000000852981 in zend_call_function (fci=fci@entry=0x7ffee921f060, fci_cache=fci_cache@entry=0x7ffee921f030) at /usr/local/src/php-7.1.9/Zend/zend_execute_API.c:855
#16 0x00000000006d3bb4 in zim_reflection_class_newInstanceArgs (execute_data=<optimized out>, return_value=0x7f205d213930)
    at /usr/local/src/php-7.1.9/ext/reflection/php_reflection.c:4996
#17 0x00000000008f7720 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1099
#18 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#19 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#20 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#21 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#22 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#23 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#24 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#25 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#26 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#27 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#28 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#29 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#30 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#31 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#32 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#33 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#34 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#35 0x00000000008f7a35 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:949
---Type <return> to continue, or q <return> to quit---return
#36 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#37 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#38 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#39 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#40 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#41 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#42 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429
#43 0x00000000008f9a44 in zend_execute (op_array=0x7f205d277000, op_array@entry=0x7f204c1df898, return_value=return_value@entry=0x7f205d213860)
    at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:474
#44 0x0000000000861d54 in zend_execute_scripts (type=type@entry=8, retval=0x7f205d213860, retval@entry=0x0, file_count=file_count@entry=3)
    at /usr/local/src/php-7.1.9/Zend/zend.c:1480
#45 0x0000000000802e60 in php_execute_script (primary_file=primary_file@entry=0x7ffee92218d0) at /usr/local/src/php-7.1.9/main/main.c:2552
#46 0x00000000008fbbdf in do_cli (argc=3, argv=0x1231830) at /usr/local/src/php-7.1.9/sapi/cli/php_cli.c:993
#47 0x0000000000448d80 in main (argc=3, argv=0x1231830) at /usr/local/src/php-7.1.9/sapi/cli/php_cli.c:1381

通过简单的堆栈信息, 我们大概可以看到一些异常点.

#39 0x00000000008f75b5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:1076
#40 0x00000000008a6a9b in execute_ex (ex=<optimized out>) at /usr/local/src/php-7.1.9/Zend/zend_vm_execute.h:429

这里大量反复的调用了 execute_ex , 通过搜索了解此为最核心的执行opcode的函数。

安装opcache、xdebug等可能会出现段错误问题, 具体可查看下面参考连接。 于是先将 opcache 禁用, 本地环境正常。

因为目前自己知识体系欠缺, 对内核了解不够, 此文先到此为止了.

参考资料: https://github.com/laravel/framework/issues/27030

原文发布于微信公众号 - 呆呆熊一点通(gh_93f28f51010a)

原文发表时间:2019-05-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券