debugging.html revision bf8889c98dd1b7b4daf701cdf96652d5c9afb837
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<HTML>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<HEAD>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<TITLE>Debugging Memory Allocation in APR</TITLE>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen</HEAD>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<BODY
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen BGCOLOR="#FFFFFF"
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen TEXT="#000000"
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen LINK="#0000FF"
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen VLINK="#000080"
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen ALINK="#FF0000"
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<!--#include virtual="header.html" -->
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<H1 ALIGN="CENTER">Debugging Memory Allocation in APR<br></H1>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<p>The allocation mechanism's within APR have a number of debugging
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenmodes that can be used to assist in finding memory problems. This document describes
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenthe modes available and gives instructions on activating them.</p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<ul>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<li><a href="#options">Available debugging options</a></li>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<li><a href="#combo">Allowable combinations</a></li>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<li><a href="#howto">How to activate debugging</a></li>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</ul>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<hr>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<a name="options">
5da08ab71623953f248b24a21d45b02555bbb24bTimo Sirainen<h2>Allocation Debugging</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>ALLOC_DEBUG</h3>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<p><em>Debugging support: Define this to enable code which helps detect re-use of freed memory and other such nonsense.</em></p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<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>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h2>Malloc Support</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>ALLOC_USE_MALLOC</h3>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<p><em>If defined all allocations will be done with malloc and free()d appropriately at the end.
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</em></p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<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>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h2>Pool Debugging</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>POOL_DEBUG</h3>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<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>
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen<p>In particular, it causes the table_{set,add,merge}n routines to check that their arguments are safe for the ap_table_t they're being placed in. It currently only works with the unix multiprocess model, but could be extended to others.</p>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h2>Table Debugging</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>MAKE_TABLE_PROFILE</h3>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<p><em>Provide diagnostic information about make_table() calls which are possibly too small.</em></p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
f39a06c378f6ea80a4ae9d257f0d79221a945a57Timo Sirainen<p>This requires a recent gcc which supports __builtin_return_address(). The error_log output will be a message such as: </p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<pre>table_push: ap_table_t created by 0x804d874 hit limit of 10</pre>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<p>Use "<em><strong>l *0x804d874</strong></em>" to find the source that corresponds to. It
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen indicates that a ap_table_t allocated by a call at that address has possibly too small an initial ap_table_t size guess.</p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h2>Allocation Statistics</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>ALLOC_STATS</h3>
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen<p><em>Provide some statistics on the cost of allocations.</em></p>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<p>This requires a bit of an understanding of how alloc.c works.</p>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<hr>
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<a name="combo">
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen<h2>Allowable Combinations</h2>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<p>Not all the options outlined above can be activated at the same time. the following table gives more information.</p>
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<p align="center">
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<table width="80%">
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<tr>
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi<th width="25%">Option 1</th>
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen<th width="15%">ALLOC<br>DEBUG</th>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<th width="15%">ALLOC<br>USE<br>MALLOC</th>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<th width="15%">POOL<br>DEBUG</th>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<th width="15%">MAKE<br>TABLE<br>PROFILE</th>
2e07e3182f355cf04a1461dd7f893d0ebc818764Timo Sirainen<th width="15%">ALLOC<br>STATS</th>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen</tr>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen<tr>
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen<td>ALLOC_DEBUG</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td bgcolor="#ff0000">&nbsp;</td>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen<td align="center">No</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td align="center">Yes</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td align="center">Yes</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td align="center">Yes</td>
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen</tr>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<tr>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td>ALLOC_USE<br>MALLOC</td>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen<td align="center">No</td>
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi<td bgcolor="#ff0000">&nbsp;</td>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen<td align="center">No</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td align="center">No</td>
bb11a1957aefbd2a2edf7ae25af4032899c34c41Martti Rannanjärvi<td align="center">No</td>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen</tr>
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi<tr>
a022e47f45597650f71f00c3af3fa783982a65adTimo Sirainen<td>POOL_DEBUG</td>
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen<td align="center">Yes</td>
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen<td align="center">No</td>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<td bgcolor="#ff0000">&nbsp;</td>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<td align="center">Yes</td>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<td align="center">Yes</td>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</tr>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<tr>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td>MAKE_TABLE<br>PROFILE</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">Yes</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">No</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">Yes</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td bgcolor="#ff0000">&nbsp;</td>
0d1b8b6bec79746c5d89d57dd8c1688946bd9237Josef 'Jeff' Sipek<td align="center">Yes</td>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen</tr>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<tr>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td>ALLOC_STATS</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">Yes</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">No</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">Yes</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td align="center">Yes</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen<td bgcolor="#ff0000">&nbsp;</td>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen</tr>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen</table>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<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>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<hr>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<a name="howto">
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<h2>Activating Debugging Options</h2>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<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 src/lib/apr/inclide/apr_general.h)</em></p>
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<pre>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen/*
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define ALLOC_DEBUG
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define POOL_DEBUG
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define ALLOC_USE_MALLOC
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define MAKE_TABLE_PROFILE
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen#define ALLOC_STATS
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen*/
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainentypedef struct ap_pool_t {
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen union block_hdr *first;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen union block_hdr *last;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct cleanup *cleanups;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct process_chain *subprocesses;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct ap_pool_t *sub_pools;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct ap_pool_t *sub_next;
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen struct ap_pool_t *sub_prev;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct ap_pool_t *parent;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen char *free_first_avail;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#ifdef ALLOC_USE_MALLOC
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen void *allocation_list;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#endif
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#ifdef POOL_DEBUG
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct ap_pool_t *joined;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#endif
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen int (*apr_abort)(int retcode);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct datastruct *prog_data;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen}ap_pool_t;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</pre>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
5de0c65da362236080fa699af3da03e45e480ab8Timo Sirainen<p>To enable allocation debugging simply move the #define ALLOC_DEBUG above the start of the comments block and rebuild the server.</p>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<h3>NB. In order to use the various options the server MUST be rebuilt after editing the header file.
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</h3>
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen<!--#include virtual="footer.html" -->
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen</body>
8705e45564a2e87d32bd825e0e997a8177846f77Timo Sirainen</html>
588a0579058849aed9f7b59d8259e0c58d9fd23cTimo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen