为什么我在这个 PHP 命令行脚本中出现分段错误?
这只是一个向我所说的“心理日志文件”发送消息的快速脚本。 只是为了在我昏昏欲睡时记录我的想法并让自己重新开始工作。 不管怎样,它在大多数情况下都工作正常,但时不时地我会遇到分段错误。 在 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);
这是我在几次运行中得到的结果:
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
知道我在这里可能做错了什么吗? 即使在产生分段错误的运行中,该文件似乎也按预期正确更新。 我正在运行 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
提前致谢。
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:
#!/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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的扩展很可能会出现某种混乱。 如果扩展是为不同版本的 php 或外部库构建的,则可能会发生这种情况。 尝试禁用 php.ini 中的所有扩展,看看是否可以解决问题。 如果是这样,则重新启用每个扩展,一次一个,直到找到违规者。
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.
可能是因为这个错误影响了 Ubuntu 和 Debian... https ://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870
It's probably because of this bug which affects both Ubuntu and Debian... https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870
看起来像 php5-mysql 中的 Ubuntu bug #343870。 禁用 PHP mysql 和 mysqli 模块后,我不再遇到分段错误。
Looks like Ubuntu bug #343870 in php5-mysql. I don't get a segmentation fault any more with PHP mysql and mysqli modules disabled.
我见过完全相同的事情,这通常只是 PHP 错误的指示。
如果问题仍然存在,我会升级到最新版本..提交错误报告。
如果您坚持使用 ubuntu 版本,您也许可以在 ubuntu 的跟踪器中发布错误报告。
I've seen the exact same things, and it's usually just in indicator of a PHP bug.
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.
尝试更改扩展的加载顺序。 我必须在curl之前移动pgsql以消除我遇到的段错误。 我读完后尝试过:
http://linux.m2osw.com/php_cli_segmentation_fault_with_pgsql
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:
http://linux.m2osw.com/php_cli_segmentation_fault_with_pgsql
无法复制,不足为奇。 实验上,我尝试使用
fopen()
而不是file_put_contents()
附加到文件。Unable to replicate, unsurprisingly. Experimentally, I'd try appending to the file using
fopen()
instead offile_put_contents()
.由于简单的堆栈溢出,我收到此错误。 为什么除了分段错误之外我没有收到另一个错误,这是一个很好的问题,但是使用
strace
和die()
我追踪到了问题。服务器
PHP
重现问题的代码
输出
Strace 输出(以防任何人都可以将其用于任何用途):
PASTEBIN LINK
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
strace
anddie()
I traced the problem down.Server
PHP
Code to reproduce problem
Output
Strace output (in case anyone can use it for anything):
PASTEBIN LINK
尝试
strace mlog blah
看看是否能提供一些线索,说明它死时正在做什么。 就其价值而言,我无法使用您报告的相同 php 构建在 64 位 Jaunty 安装上重现该情况。
Try
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.