Archive

Posts Tagged ‘apache’

How to debug a segmentation fault caused by PHP

December 1st, 2009 4 comments

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