my life

day to day

Archive for July 15th, 2006

Using Valgrind with PHP

Saturday, July 15th, 2006

Download source version of PHP that you want to debug

Configure and Make 

./configure --prefix=/home/erik/php --enable-sigchild --with-openssl --with-zlib --with-curl --with-curlwrappers --enable-mbstring --enable-pgsql --enable-mysql --enable-pcntl --enable-sockets --enable-zend-multibyte --enable-debug --disable-zend-memory-manager make clean all install

The most important flags have been highlighted. Take care to change your installation path (prefix) to some development environment and not your system’s production environment.

Enabling debug is for GDB and not essential for Valgrind.

Run Valgrind

valgrind --tool=memcheck --leak-check=full ./sample.php

It will intercept all possible Seg Faults and attempt to maintain execution. After you terminate the application, you’ll get output that resembles:

==13501== ==13501== ERROR SUMMARY: 45 errors from 3 contexts (suppressed: 31 from 1) ==13501== malloc/free: in use at exit: 1471 bytes in 46 blocks. ==13501== malloc/free: 2199156 allocs, 2199110 frees, 81359317 bytes allocated. ==13501== For counts of detected errors, rerun with: -v ==13501== searching for pointers to 46 not-freed blocks. ==13501== checked 1004344 bytes. ==13501== ==13501== ==13501== 144 bytes in 9 blocks are definitely lost in loss record 7 of 10 ==13501==    at 0×1B908222: malloc (vg_replace_malloc.c:130) ==13501==    by 0×824DFA3: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:182) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501== ==13501== ==13501== 240 bytes in 15 blocks are definitely lost in loss record 8 of 10 ==13501==    at 0×1B908222: malloc (vg_replace_malloc.c:130) ==13501==    by 0×8257DB2: ZEND_SEND_REF_SPEC_VAR_HANDLER (zend_vm_execute.h:7194) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×824DBEB: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:234) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×822C075: zend_call_function (zend_execute_API.c:938) ==13501==    by 0×822CF7B: call_user_function_ex (zend_execute_API.c:579) ==13501==    by 0×81A4365: zif_call_user_func_array (basic_functions.c:2158) ==13501== ==13501== ==13501== 261 (16 direct, 245 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 10 ==13501==    at 0×1B908222: malloc (vg_replace_malloc.c:130) ==13501==    by 0×8227C1A: zend_do_fetch_static_variable (zend_compile.c:3380) ==13501==    by 0×8218409: zendparse (zend_language_parser.c:3483) ==13501==    by 0×821B4E0: compile_file (zend_language_scanner.c:3080) ==13501==    by 0×821F5D0: compile_filename (zend_language_scanner.c:3125) ==13501==    by 0×826FF05: ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER (zend_vm_execute.h:19498) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501==    by 0×822C075: zend_call_function (zend_execute_API.c:938) ==13501==    by 0×822CD24: zend_lookup_class_ex (zend_execute_API.c:1044) ==13501==    by 0×822CE32: zend_fetch_class (zend_execute_API.c:1443) ==13501==    by 0×824E7D0: ZEND_FETCH_CLASS_SPEC_CONST_HANDLER (zend_vm_execute.h:627) ==13501==    by 0×824D9AC: execute (zend_vm_execute.h:92) ==13501== ==13501== LEAK SUMMARY: ==13501==    definitely lost: 400 bytes in 25 blocks. ==13501==    indirectly lost: 245 bytes in 11 blocks. ==13501==      possibly lost: 0 bytes in 0 blocks. ==13501==    still reachable: 826 bytes in 10 blocks. ==13501==         suppressed: 0 bytes in 0 blocks. ==13501== Reachable blocks (those to which a pointer was found) are not shown.

As you can see, PHP is sick, leaking memory all over the place. Joy.

add to del.icio.us    add to technorati favs   email this

PHP5.1.4 Segfault Woes

Saturday, July 15th, 2006

For the last few days I’ve been battling a nasty bug that remains elusive. What makes it so difficult to track down is that it crashes at totally random spots on a vanilla PHP setup (no special PECLs). With more than 10,000 lines of highly objectified code and no decent debuggers, this is a real challenge that I’d be glad if it just solved itself.

 

add to del.icio.us    add to technorati favs   email this