php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Generating a valgrind log

Important!

To get a meaningful log you must have PHP configured with --enable-debug and disable Zend memory manager.

Disabling Zend MM

Zend Engine uses its own routines to optimize memory management, but because of this valgrind cannot see most of the memory issues. You must disable Zend memory manager before running PHP with valgrind. In order to do this you need to set USE_ZEND_ALLOC environment variable to 0.

Use

export USE_ZEND_ALLOC=0
or
setenv USE_ZEND_ALLOC 0
(the syntax depends on what your shell supports).

This works since PHP 5.2, in older versions you had to reconfigure PHP with --disable-zend-memory-manager option.

Using Shared Extensions

To correctly show the stack frames for extensions compiled as shared libraries, set:

export ZEND_DONT_UNLOAD_MODULES=1
or
setenv ZEND_DONT_UNLOAD_MODULES 1
(the syntax depends on what your shell supports).

This works from PHP 5.3.11 onwards.

Running PHP CLI, Built-in Web Server or PHP CGI through valgrind

To generate the valgrind log using PHP CLI/CGI, you need to execute the following command:

    
        valgrind --tool=memcheck --num-callers=30 --log-file=php.log /path/to/php-cli script.php
    

This should put the log into php.log file in the current working directory.

To valgrind the built-in web server, use the appropriate -S and -t options to the CLI executable. Run your application via a browser and review php.log for any valgrind errors.

Running PHP Apache module through valgrind

If you compiled PHP and Apache statically, make sure the Apache binary is not stripped after make install, otherwise you lose the required debug info. To check it, run file /path/to/httpd, it should output something like this (notice "not stripped"):

    
        # file /usr/local/apache2/bin/httpd
        /usr/local/apache2/bin/httpd: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), not stripped
    

To generate the valgrind log using PHP as Apache module, you need to run the Apache itself under valgrind:

    
        valgrind --tool=memcheck --num-callers=30 --log-file=apache.log /usr/local/apache/bin/httpd -X
    

Run your application via a browser. All the memory errors will be in apache.log.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC