©
This document usesPHP Chinese website manualRelease
The allocation mechanisms within APR have a number of debugging modes that can be used to assist in finding memory problems. This document describes the modes available and gives instructions on activating them.
free()
d memory and other such nonsense.
The theory is simple. TheFILL_BYTE
(0xa5
) is written over allmalloc
'd memory as we receive it, and is written over everything that we free up during aclear_pool
. We check that blocks on the free list always have theFILL_BYTE
in them, and we check duringpalloc()
that the bytes still haveFILL_BYTE
in them. If you ever see garbage URLs or whatnot containing lots of0xa5
s then you know something used data that's been freed or uninitialized.
malloc()
and
free()
d appropriately at the end.
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 inALLOC_DEBUG
. But don't add inALLOC_DEBUG
if you're using Purify becauseALLOC_DEBUG
would hide all the uninitialized read errors that Purify can diagnose.
In particular, it causes thetable_{set,add,merge}n
routines to check that their arguments are safe for theapr_table_t
they're being placed in. It currently only works with the unix multiprocess model, but could be extended to others.
This requires a recent gcc which supports__builtin_return_address()
. The error_log output will be a message such as:
table_push: apr_table_t created by 0x804d874 hit limit of 10
Usel *0x804d874
to find the source that corresponds to. It indicates that aapr_table_t
allocated by a call at that address has possibly too small an initialapr_table_t
size guess.
This requires a bit of an understanding of howalloc.c
works.
Not all the options outlined above can be activated at the same time. the following table gives more information.
ALLOC DEBUG | ALLOC USE MALLOC | POOL DEBUG | MAKE TABLE PROFILE | ALLOC STATS | |
---|---|---|---|---|---|
ALLOC DEBUG | - | No | Yes | Yes | Yes |
ALLOC USE MALLOC | No | - | No | No | No |
POOL DEBUG | Yes | No | - | Yes | Yes |
MAKE TABLE PROFILE | Yes | No | Yes | - | Yes |
ALLOC STATS | Yes | No | Yes | Yes | - |
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.
The various options for debugging memory are now enabled in theapr_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 (contained in srclib/apr/include/apr_pools.h)
/*
#define ALLOC_DEBUG
#define POOL_DEBUG
#define ALLOC_USE_MALLOC
#define MAKE_TABLE_PROFILE
#define ALLOC_STATS
*/
typedef struct ap_pool_t {
union block_hdr *first;
union block_hdr *last;
struct cleanup *cleanups;
struct process_chain *subprocesses;
struct ap_pool_t *sub_pools;
struct ap_pool_t *sub_next;
struct ap_pool_t *sub_prev;
struct ap_pool_t *parent;
char *free_first_avail;
#ifdef ALLOC_USE_MALLOC
void *allocation_list;
#endif
#ifdef POOL_DEBUG
struct ap_pool_t *joined;
#endif
int (*apr_abort)(int retcode);
struct datastruct *prog_data;
} ap_pool_t;
To enable allocation debugging simply move the#define ALLOC_DEBUG
above the start of the comments block and rebuild the server.
In order to use the various options the servermustbe rebuilt after editing the header file.