为什么我在这个 PHP 命令行脚本中出现这个分段错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1106166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Do I Get This Segmentation Fault In This PHP Command Line Script?
提问by Ali
It's just a quick script to a message to what I call a 'mental log file'. Just to keep track of my thoughts when I drift off and get myself back to work. Anyway, it works alright most of the time, but every so often I get a segmentation fault. Heard of them in C, but never had them before in PHP. Here's the script:
这只是一个快速脚本,我称之为“心理日志文件”。只是为了在我离开时跟踪我的想法并让自己重新开始工作。无论如何,它大部分时间都可以正常工作,但每隔一段时间我就会遇到分段错误。在 C 中听说过它们,但以前从未在 PHP 中使用过它们。这是脚本:
#!/usr/bin/php
<?php
$mental_log_file = "/home/ali/mental-log";
array_shift($argv); //get rid of the initial arg (name of the command)
$log_entry = date('j-n-y H:i') . ' ' . implode(' ', $argv) . "\n";
file_put_contents($mental_log_file, $log_entry, FILE_APPEND);
Here's what I get on a few runs:
这是我在几次运行中得到的结果:
ali@oem-desktop:~$ mlog blah ali@oem-desktop:~$ mlog blah Segmentation fault ali@oem-desktop:~$ mlog blah ali@oem-desktop:~$ mlog blah Segmentation fault ali@oem-desktop:~$ mlog blah ali@oem-desktop:~$ mlog blah ali@oem-desktop:~$ mlog blah Segmentation fault ali@oem-desktop:~$ mlog blah ali@oem-desktop:~$ mlog blah Segmentation fault ali@oem-desktop:~$ mlog blah Segmentation fault
Any idea of what I could be doing wrong here? The file seems to be updating correctly as expected even on the runs that produce segmentation faults. I'm running Ubuntu 9.04 Jaunty.
知道我在这里可能做错了什么吗?即使在产生分段错误的运行中,该文件似乎也按预期正确更新。我正在运行 Ubuntu 9.04 Jaunty。
ali@oem-desktop:~$ php --version PHP 5.2.6-3ubuntu4.1 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 23 2009 14:37:14) Copyright (c) 1997-2008 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
Thanks in advance.
提前致谢。
回答by Paul Dixon
Try
尝试
strace mlog blah
strace mlog blah
and see if that gives some clues as what it is doing when it dies. For what it's worth, I couldn't reproduce that on a 64 bit Jaunty install with the same php build you report.
看看这是否提供了一些线索,说明它死后在做什么。对于它的价值,我无法在 64 位 Jaunty 安装上使用您报告的相同 php 构建重现它。
回答by troelskn
It's very likely that you have an extension which somehow messes up. That can happen if the extension was built for a different version of php or of an external library. Try to disable all extensions in php.ini and see if it solves the problem. If it does, then re-enable each extension, one at a time, until you find the offender.
您很可能有一个不知何故搞砸的扩展。如果扩展是为不同版本的 php 或外部库构建的,就会发生这种情况。尝试禁用 php.ini 中的所有扩展,看看是否能解决问题。如果是,则重新启用每个扩展,一次一个,直到找到违规者。
回答by Fuzzy76
It's probably because of this bug which affects both Ubuntu and Debian... https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870
可能是因为这个 bug 影响了 Ubuntu 和 Debian... https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870
回答by Fuzzy76
Looks like Ubuntu bug #343870in php5-mysql. I don't get a segmentation fault any more with PHP mysql and mysqli modules disabled.
看起来像php5-mysql 中的Ubuntu 错误 #343870。禁用 PHP mysql 和 mysqli 模块后,我不再遇到分段错误。
回答by Liam Morland
Try changing the load order of extension. I had to move pgsql before curl to get rid of a segfault I was having. I tried it after reading this:
尝试更改扩展的加载顺序。我不得不在 curl 之前移动 pgsql 以摆脱我遇到的段错误。读完这篇后我试了一下:
http://linux.m2osw.com/php_cli_segmentation_fault_with_pgsql
http://linux.m2osw.com/php_cli_segmentation_fault_with_pgsql
回答by Evert
I've seen the exact same things, and it's usually just in indicator of a PHP bug.
我见过完全相同的事情,而且通常只是表明存在 PHP 错误。
I would upgrade to the latest version, if the problem persists.. file a bug report. If you are stuck to ubuntu versions, you might be able to post a bug report in ubuntu's tracker instead.
如果问题仍然存在,我会升级到最新版本。提交错误报告。如果您坚持使用 ubuntu 版本,则可以改为在 ubuntu 的跟踪器中发布错误报告。
回答by chaos
Unable to replicate, unsurprisingly. Experimentally, I'd try appending to the file using fopen()instead of file_put_contents().
毫无疑问,无法复制。实验上,我会尝试使用fopen()而不是file_put_contents().
回答by h2ooooooo
I got this error due to a simple stack overflow. Why I didn't get another error than a segmentation fault is a good question, but using straceand die()I traced the problem down.
由于简单的堆栈溢出,我收到此错误。为什么我没有得到除分段错误之外的另一个错误是一个很好的问题,但是使用strace并die()我追踪了问题。
Server
服务器
Debian 6.0 (squeeze)
Linux domain.com 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64 GNU/Linux
Debian 6.0(挤压)
Linux domain.com 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64 GNU/Linux
PHP
PHP
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli) (built: Aug 6 2012 14:18:06)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli)(构建时间:2012 年 8 月 6 日 14:18:06)
版权所有 (c) 1997-2009 PHP Group
Zend Engine v2.3.0,版权所有 (c) 1998-2010 Zend Technologies
Suhosin v0.9.32.1,版权所有 (c) 2007-2010,SektionEins GmbH
Code to reproduce problem
重现问题的代码
<?php
function Bar() {
return Bar();
}
Bar();
?>
Output
输出
php5 overflowtest.php
Segmentation fault
php5 溢出测试.php
分段故障

