6

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.

1
  • 4
    I have some pretty trivial PHP cli scripts that segfault seemingly at random, too. Commented Jan 12, 2010 at 2:32

8 Answers 8

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that! Tried running it but I'm not so clued up on system calls/low level programming, couldn't really make sense of the output.
Paste an strace of a typical crash into pastebin.com and add a link to it in your question
Also, troelskn is probably onto something - what extensions have you got enabled?
wow... this saved me man... could see it was to do with Autoloading going on... strace is a big help
4

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.

Comments

3

It's probably because of this bug which affects both Ubuntu and Debian... https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870

Comments

2

Looks like Ubuntu bug #343870 in php5-mysql. I don't get a segmentation fault any more with PHP mysql and mysqli modules disabled.

Comments

1

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.

Comments

1

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

Comments

0

Unable to replicate, unsurprisingly. Experimentally, I'd try appending to the file using fopen() instead of file_put_contents().

Comments

-1

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 and die() I traced the problem down.

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

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

Code to reproduce problem

<?php   
    function Bar() {
        return Bar();
    }
    Bar();
?>

Output

php5 overflowtest.php

Segmentation fault

Strace output (in case anyone can use it for anything):

PASTEBIN LINK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.