]> granicus.if.org Git - apache/blob - docs/manual/developer/debugging.html
More of s/ap_table_t/apr_table_t/g;
[apache] / docs / manual / developer / debugging.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>Debugging Memory Allocation in APR</TITLE>
5 </HEAD>
6
7 <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
8 <BODY
9  BGCOLOR="#FFFFFF"
10  TEXT="#000000"
11  LINK="#0000FF"
12  VLINK="#000080"
13  ALINK="#FF0000"
14 >
15
16 <!--#include virtual="header.html" -->
17
18 <H1 ALIGN="CENTER">Debugging Memory Allocation in APR<br></H1>
19
20 <p>The allocation mechanism's within APR have a number of debugging
21 modes that can be used to assist in finding memory problems.  This document describes
22 the modes available and gives instructions on activating them.</p>
23
24 <ul>
25 <li><a href="#options">Available debugging options</a></li>
26 <li><a href="#combo">Allowable combinations</a></li>
27 <li><a href="#howto">How to activate debugging</a></li>
28 </ul>
29
30 <hr>
31 <a name="options">
32 <h2>Allocation Debugging</h2>
33
34 <h3>ALLOC_DEBUG</h3>
35 <p><em>Debugging support: Define this to enable code which helps detect re-use of freed memory and other such nonsense.</em></p>
36
37 <p>The theory is simple.  The FILL_BYTE (0xa5) is written over all malloc'd memory as we receive it, and is written over everything that we free up during a clear_pool.  We check that blocks on the free list always have the FILL_BYTE in them, and we check during palloc() that the bytes still have FILL_BYTE in them.  If you ever see garbage URLs or whatnot containing lots of 0xa5s then you know something used data that's been freed or uninitialized.</p>
38
39 <h2>Malloc Support</h2>
40 <h3>ALLOC_USE_MALLOC</h3>
41 <p><em>If defined all allocations will be done with malloc and free()d appropriately at the end.
42 </em></p>
43
44 <p>This is intended to be used with something like Electric Fence or Purify to help detect memory problems.  Note that if you're using efence then you should also add in ALLOC_DEBUG.  But don't add in ALLOC_DEBUG if you're using Purify because ALLOC_DEBUG would hide all the uninitialized read errors that Purify can diagnose.</p>
45
46 <h2>Pool Debugging</h2>
47 <h3>POOL_DEBUG</h3>
48 <p><em>This is intended to detect cases where the wrong pool is used when assigning data to an object in another pool.</em></p>
49
50 <p>In particular, it causes the table_{set,add,merge}n routines to check that their arguments are safe for the apr_table_t they're being placed in.  It currently only works with the unix multiprocess model, but could be extended to others.</p>
51
52 <h2>Table Debugging</h2>
53 <h3>MAKE_TABLE_PROFILE</h3>
54 <p><em>Provide diagnostic information about make_table() calls which are possibly too small.</em></p>
55
56 <p>This requires a recent gcc which supports __builtin_return_address().  The error_log output will be a message such as: </p>
57 <pre>table_push: apr_table_t created by 0x804d874 hit limit of 10</pre>
58 <p>Use "<em><strong>l *0x804d874</strong></em>" to find the source that corresponds to.  It
59  indicates that a apr_table_t allocated by a call at that address has possibly too small an initial apr_table_t size guess.</p>
60
61 <h2>Allocation Statistics</h2>
62 <h3>ALLOC_STATS</h3>
63 <p><em>Provide some statistics on the cost of allocations.</em></p>
64
65 <p>This requires a bit of an understanding of how alloc.c works.</p>
66
67 <hr>
68
69 <a name="combo">
70 <h2>Allowable Combinations</h2>
71
72 <p>Not all the options outlined above can be activated at the same time.  the following table gives more information.</p>
73
74 <p align="center">
75 <table width="80%">
76 <tr>
77 <th width="25%">Option 1</th>
78 <th width="15%">ALLOC<br>DEBUG</th>
79 <th width="15%">ALLOC<br>USE<br>MALLOC</th>
80 <th width="15%">POOL<br>DEBUG</th>
81 <th width="15%">MAKE<br>TABLE<br>PROFILE</th>
82 <th width="15%">ALLOC<br>STATS</th>
83 </tr>
84 <tr>
85 <td>ALLOC_DEBUG</td>
86 <td bgcolor="#ff0000">&nbsp;</td>
87 <td align="center">No</td>
88 <td align="center">Yes</td>
89 <td align="center">Yes</td>
90 <td align="center">Yes</td>
91 </tr>
92 <tr>
93 <td>ALLOC_USE<br>MALLOC</td>
94 <td align="center">No</td>
95 <td bgcolor="#ff0000">&nbsp;</td>
96 <td align="center">No</td>
97 <td align="center">No</td>
98 <td align="center">No</td>
99 </tr>
100 <tr>
101 <td>POOL_DEBUG</td>
102 <td align="center">Yes</td>
103 <td align="center">No</td>
104 <td bgcolor="#ff0000">&nbsp;</td>
105 <td align="center">Yes</td>
106 <td align="center">Yes</td>
107 </tr>
108 <tr>
109 <td>MAKE_TABLE<br>PROFILE</td>
110 <td align="center">Yes</td>
111 <td align="center">No</td>
112 <td align="center">Yes</td>
113 <td bgcolor="#ff0000">&nbsp;</td>
114 <td align="center">Yes</td>
115 </tr>
116 <tr>
117 <td>ALLOC_STATS</td>
118 <td align="center">Yes</td>
119 <td align="center">No</td>
120 <td align="center">Yes</td>
121 <td align="center">Yes</td>
122 <td bgcolor="#ff0000">&nbsp;</td>
123 </tr>
124
125 </table>
126
127 <p>Additionally the debugging options are not suitable for multi-threaded versions of the server.  When trying to debug with these options the server should be started in single process mode.</p>
128
129 <hr>
130
131 <a name="howto">
132 <h2>Activating Debugging Options</h2>
133 <p>The various options for debugging memory are now enabled in the apr_general.h header file in APR.  The various options are enabled by uncommenting the define for the option you wish to use.  The section of the code currently looks like this <em>(contained in srclib/apr/include/apr_pools.h)</em></p>
134
135 <pre>
136 /*
137 #define ALLOC_DEBUG
138 #define POOL_DEBUG
139 #define ALLOC_USE_MALLOC
140 #define MAKE_TABLE_PROFILE
141 #define ALLOC_STATS
142 */
143
144 typedef struct ap_pool_t {
145     union block_hdr *first;
146     union block_hdr *last;
147     struct cleanup *cleanups;
148     struct process_chain *subprocesses;
149     struct ap_pool_t *sub_pools;
150     struct ap_pool_t *sub_next;
151     struct ap_pool_t *sub_prev;
152     struct ap_pool_t *parent;
153     char *free_first_avail;
154 #ifdef ALLOC_USE_MALLOC
155     void *allocation_list;
156 #endif
157 #ifdef POOL_DEBUG
158     struct ap_pool_t *joined;
159 #endif
160     int (*apr_abort)(int retcode);
161     struct datastruct *prog_data;
162 }ap_pool_t;
163 </pre>
164
165 <p>To enable allocation debugging simply move the #define ALLOC_DEBUG above the start of the comments block and rebuild the server.</p>
166
167 <h3>NB. In order to use the various options the server MUST be rebuilt after editing the header file.
168 </h3>
169
170 <!--#include virtual="footer.html" -->
171
172 </body>
173 </html>
174
175
176
177
178