Detecting php extended memory leakage with valgrind

Source: Internet
Author: User
Tags apache php php cli php mod valgrind

Original article: https://bugs.php.net/bugs-getting-valgrind-log.php

Prerequisites

1. When compiling php, you must include-- Enable-debug option.

2. Disable php memory management.

Disable Zend MM

The Zend virtual machine uses its own program to optimize memory management. Therefore, valgrind cannot detect most memory problems. Before using valgrind to execute php, you must disable the Memory Manager that comes with Zend. Disable the environment variable USE_ZEND_ALLOC to 0.

export USE_ZEND_ALLOC=0

Or

setenv USE_ZEND_ALLOC 0

The above method applies to php5.2 and later versions. Php versions earlier than 5.2 must be included during compilation.--disable-zend-memory-manager.

Use shared Extension

To correctly display the extension memory stack in valgrind, you need to set:

export ZEND_DONT_UNLOAD_MODULES=1

Or

setenv ZEND_DONT_UNLOAD_MODULES 1

This setting applies to PHP 5.3.11 and later versions.

Editor's note: for example, if you do not setZEND_DONT_UNLOAD_MODULES, Valgrind may report

$ valgrind --leak-check=full --show-reachable=yes php test.php
...
==25829== 8 bytes in 1 blocks are indirectly lost in loss record 2 of 21
==25829==    at 0x4C25E84: ???
==25829==    by 0xCE440DC: ???
==25829==    by 0xCE44316: ???
==25829==    by 0xCE44368: ???
==25829==    by 0xCBEE55F: ???
==25829==    by 0xCBD3F87: ???
==25829==    by 0x949A85: zend_activate_modules (zend_API.c:2285)
==25829==    by 0x8B5EBC: php_request_startup (main.c:1491)
==25829==    by 0xA84F7B: main (php_cli.c:1356)
...

If you setZEND_DONT_UNLOAD_MODULESIs displayed as follows:

$ valgrind --leak-check=full --show-reachable=yes php test.php
...
==25824== 8 bytes in 1 blocks are still reachable in loss record 2 of 30
==25824==    at 0x4C25E84: calloc (in /usr/lib/valgrind/vgpreload_memcheck.so)
==25824==    by 0xCE440DC: event_base_priority_init (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCE44316: event_base_new (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCE44368: event_init (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCBEE55F: zm_activate_http_request_pool (http_request_pool_api.c:58)
==25824==    by 0xCBD3F87: zm_activate_http (http.c:373)
==25824==    by 0x949A85: zend_activate_modules (zend_API.c:2285)
==25824==    by 0x8B5EBC: php_request_startup (main.c:1491)
==25824==    by 0xA84F7B: main (php_cli.c:1356)
...
Use CLI, web server built-in or CGI to execute php

To generate valgrind logs for php CLI/CGI, run the following command:

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

This will output the log to the php. log file in the current directory.

If you want to detect the built-in php of web server, you need to use the appropriate-S and-t parameters for the CLI executable file. Then execute the request in the browser, and then check the valgrind error in php. log.

Use valgrind to execute PHP Apache module

If you compile php and apache statically, make sure that the apache bin is not separated after make install. Otherwise, the required debugging information will be lost. Check as follows: Execute/Path/to/httpd, which outputs something (for example, 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

If you want to generate a valgrind detection report for apache php mod, you need to run apache under valgrind:

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

All memory errors are output to apache. log through browser requests.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.