1<?php $this->extends('layout.php', ['title' => 'Generating a valgrind log']) ?>
2
3<?php $this->start('content') ?>
4
5<h1>Generating a valgrind log</h1>
6
7<h3>Important!</h3>
8
9To get a meaningful log you must have PHP configured with <code>--enable-debug</code>
10and disable Zend memory manager.
11
12<h3>Disabling Zend MM</h3>
13
14<p>
15Zend Engine uses its own routines to optimize memory management, but because of
16this valgrind cannot see most of the memory issues. You must disable Zend memory
17manager before running PHP with valgrind. In order to do this you need to set
18USE_ZEND_ALLOC environment variable to 0.
19</p>
20
21<p>
22    Use
23    <pre><code>export USE_ZEND_ALLOC=0</code></pre>
24    or
25    <pre><code>setenv USE_ZEND_ALLOC 0</code></pre>
26    (the syntax depends on what your shell supports).
27</p>
28
29<p>
30This works since PHP 5.2, in older versions you had to reconfigure PHP with
31<code>--disable-zend-memory-manager</code> option.
32</p>
33
34<h3>Using Shared Extensions</h3>
35
36<p>
37To correctly show the stack frames for extensions compiled as shared libraries, set:
38<pre><code>export ZEND_DONT_UNLOAD_MODULES=1</code></pre> or
39<pre><code>setenv ZEND_DONT_UNLOAD_MODULES 1</code></pre> (the syntax depends on
40what your shell supports).
41</p>
42
43<p>
44This works from PHP 5.3.11 onwards.
45</p>
46
47<h3>Running PHP CLI, Built-in Web Server or PHP CGI through valgrind</h3>
48
49<p>
50To generate the valgrind log using PHP CLI/CGI, you need to execute the following
51command:
52</p>
53
54<pre>
55    <code>
56        valgrind --tool=memcheck --num-callers=30 --log-file=php.log /path/to/php-cli script.php
57    </code>
58</pre>
59
60<p>This should put the log into php.log file in the current working directory.</p>
61
62<p>
63To valgrind the built-in web server, use the appropriate -S and -t options to
64the CLI executable. Run your application via a browser and review php.log for
65any valgrind errors.
66</p>
67
68<h3>Running PHP Apache module through valgrind</h3>
69
70<p> If you compiled PHP and Apache statically, make sure the Apache binary is
71not stripped after <code>make install</code>, otherwise you lose the required
72debug info. To check it, run <code>file /path/to/httpd</code>, it should output
73something like this (notice &quot;not stripped&quot;):
74</p>
75
76<pre>
77    <code>
78        # file /usr/local/apache2/bin/httpd
79        /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
80    </code>
81</pre>
82
83<p>
84To generate the valgrind log using PHP as Apache module, you need to run the
85Apache itself under valgrind:
86</p>
87
88<pre>
89    <code>
90        valgrind --tool=memcheck --num-callers=30 --log-file=apache.log /usr/local/apache/bin/httpd -X
91    </code>
92</pre>
93
94<p>
95Run your application via a browser. All the memory errors will be in apache.log.
96</p>
97
98<?php $this->end('content') ?>
99