1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
6 <relativepath href=".."/>
8 <title>Debugging Memory Allocation in APR</title>
11 <p>The allocation mechanism's within APR have a number of debugging modes
12 that can be used to assist in finding memory problems. This document
13 describes the modes available and gives instructions on activating
17 <section id="options"><title>Available debugging options</title>
18 <section id="alloc_debug">
19 <title>Allocation Debugging - ALLOC_DEBUG</title>
21 <note>Debugging support: Define this to enable code which
22 helps detect re-use of <code>free()</code>d memory and other such
25 <p>The theory is simple. The <code>FILL_BYTE</code> (<code>0xa5</code>)
26 is written over all <code>malloc</code>'d memory as we receive it, and
27 is written over everything that we free up during a
28 <code>clear_pool</code>. We check that blocks on the free list always
29 have the <code>FILL_BYTE</code> in them, and we check during
30 <code>palloc()</code> that the bytes still have <code>FILL_BYTE</code>
31 in them. If you ever see garbage URLs or whatnot containing lots
32 of <code>0xa5</code>s then you know something used data that's been
33 freed or uninitialized.</p>
36 <section id="alloc_use_malloc">
37 <title>Malloc Support - ALLOC_USE_MALLOC</title>
39 <note>If defined all allocations will be done with
40 <code>malloc()</code> and <code>free()</code>d appropriately at the
43 <p>This is intended to be used with something like Electric
44 Fence or Purify to help detect memory problems. Note that if
45 you're using efence then you should also add in <code>ALLOC_DEBUG</code>.
46 But don't add in <code>ALLOC_DEBUG</code> if you're using Purify because
47 <code>ALLOC_DEBUG</code> would hide all the uninitialized read errors
48 that Purify can diagnose.</p>
51 <section id="pool_debug"><title>Pool Debugging - POOL_DEBUG</title>
52 <note>This is intended to detect cases where the wrong pool is
53 used when assigning data to an object in another pool.</note>
55 <p>In particular, it causes the <code>table_{set,add,merge}n</code>
56 routines to check that their arguments are safe for the
57 <code>apr_table_t</code> they're being placed in. It currently only works
58 with the unix multiprocess model, but could be extended to others.</p>
61 <section id="make_table_profile">
62 <title>Table Debugging - MAKE_TABLE_PROFILE</title>
64 <note>Provide diagnostic information about make_table() calls
65 which are possibly too small.</note>
67 <p>This requires a recent gcc which supports
68 <code>__builtin_return_address()</code>. The error_log output will be a
71 table_push: apr_table_t created by 0x804d874 hit limit of 10
74 <p>Use <code>l *0x804d874</code> to find the
75 source that corresponds to. It indicates that a <code>apr_table_t</code>
76 allocated by a call at that address has possibly too small an
77 initial <code>apr_table_t</code> size guess.</p>
80 <section id="alloc_stats">
81 <title>Allocation Statistics - ALLOC_STATS</title>
83 <note>Provide some statistics on the cost of allocations.</note>
85 <p>This requires a bit of an understanding of how alloc.c works.</p>
89 <section id="combo"><title>Allowable Combinations</title>
91 <p>Not all the options outlined above can be activated at the
92 same time. the following table gives more information.</p>
94 <table border="1" style="zebra">
97 <th>ALLOC USE MALLOC</th>
99 <th>MAKE TABLE PROFILE</th>
100 <th>ALLOC STATS</th></tr>
101 <tr><th>ALLOC DEBUG</th>
102 <td>-</td><td>No</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
103 <tr><th>ALLOC USE MALLOC</th>
104 <td>No</td><td>-</td><td>No</td><td>No</td><td>No</td></tr>
105 <tr><th>POOL DEBUG</th>
106 <td>Yes</td><td>No</td><td>-</td><td>Yes</td><td>Yes</td></tr>
107 <tr><th>MAKE TABLE PROFILE</th>
108 <td>Yes</td><td>No</td><td>Yes</td><td>-</td><td>Yes</td></tr>
109 <tr><th>ALLOC STATS</th>
110 <td>Yes</td><td>No</td><td>Yes</td><td>Yes</td><td>-</td></tr>
113 <p>Additionally the debugging options are not suitable for
114 multi-threaded versions of the server. When trying to debug
115 with these options the server should be started in single
119 <section id="howto"><title>Activating Debugging Options</title>
121 <p>The various options for debugging memory are now enabled in
122 the <code>apr_general.h</code> header file in APR. The various options are
123 enabled by uncommenting the define for the option you wish to
124 use. The section of the code currently looks like this
125 (<em>contained in srclib/apr/include/apr_pools.h</em>)</p>
129 #define ALLOC_DEBUG<br />
130 #define POOL_DEBUG<br />
131 #define ALLOC_USE_MALLOC<br />
132 #define MAKE_TABLE_PROFILE<br />
133 #define ALLOC_STATS<br />
136 typedef struct ap_pool_t {<br />
138 union block_hdr *first;<br />
139 union block_hdr *last;<br />
140 struct cleanup *cleanups;<br />
141 struct process_chain *subprocesses;<br />
142 struct ap_pool_t *sub_pools;<br />
143 struct ap_pool_t *sub_next;<br />
144 struct ap_pool_t *sub_prev;<br />
145 struct ap_pool_t *parent;<br />
146 char *free_first_avail;<br />
148 #ifdef ALLOC_USE_MALLOC<br />
150 void *allocation_list;<br />
153 #ifdef POOL_DEBUG<br />
155 struct ap_pool_t *joined;<br />
159 int (*apr_abort)(int retcode);<br />
160 struct datastruct *prog_data;<br />
165 <p>To enable allocation debugging simply move the <code>#define
166 ALLOC_DEBUG</code> above the start of the comments block and rebuild
169 <note><title>Note</title>
170 <p>In order to use the various options the server <strong>must</strong>
171 be rebuilt after editing the header file.</p>