Home > Applications, Linux, Tips & Tricks > How to debug a segmentation fault caused by PHP

How to debug a segmentation fault caused by PHP

December 1st, 2009

A segmentation fault can have many causes, the best thing to do when you have a segmentation fault is debug it to find out what’s causing it.

I explain to ways two do this.

With The GNU Debugger

  • First install the gdb package with apt-get or yum.
  • Second, stop all httpd processes
  • Start Apache in debug mode

Redhat

httpd -X

Debian

apache2 -X
  • Find the parent process id from Apache

Redhat

cat /var/run/httpd.pid

Debian

cat /var/run/apache.pid
  • Start the gdb program
gdb
  • Connect the GNU debugger to the Apache process
attach <apache process id>
  • The debugger will halt the process, but we want to run it till the segmentation fault has happened
continue
  • Now try to reproduce the segmentation fault. When this happens you will see the fault in the debugger.
  • To see what happened, get the backtrace
bt

With Valgrind

  • Install valgrind with yum or apt-get
  • Stop all Apache processes
  • Start valgrind with Apache in debug mode

Redhat

valgrind /usr/sbin/httpd -X

Debian

valgrind /usr/sbin/apache2 -X
  • Try to reproduce the segmentation fault and valgrind shows what happened
  1. Martin
    December 20th, 2010 at 14:48 | #1

    Thanks for this useful info. However, when I call apache2 -X on debian lenny it tells me “apache2: bad user name ${APACHE_RUN_USER}”.

  2. June 14th, 2012 at 15:19 | #2

    Thanks. It’s been useful.

  3. Kamal Kishore
    January 3rd, 2013 at 07:59 | #3

    @Martin: i had the same problem and i changed the User and Group to www-data in apache2.conf file

  4. greenone
    May 17th, 2013 at 15:59 | #4

    hi, just a tipp:

    sudo nano /usr/sbin/apache2ctl

    look for the definition of “start”, e.g:

    start)
    mkdir -p ${APACHE_RUN_DIR:-/var/run/apache2}
    mkdir_chown ${APACHE_RUN_USER:-www-data} ${APACHE_LOCK_DIR:-/var/lock/apache2}
    # ssl_scache shouldn’t be here if we’re just starting up.
    # (this is bad if there are several apache2 instances running)
    rm -f ${APACHE_RUN_DIR:-/var/run/apache2}/*ssl_scache*
    $HTTPD ${APACHE_ARGUMENTS} -k $ARGV
    ERROR=$?
    ;;

    copy it and replace “-k $ARGV” with “-X -k start”

    not stop apache and use apache2ctl debug to run it in debug mode

  1. No trackbacks yet.
Comments are closed.