The GNU C library lets you modify the behavior of malloc,
realloc, and free by specifying appropriate hook
functions. You can use these hooks to help you debug programs that use
dynamic memory allocation, for example.
The hook variables are declared in malloc.h.
— Variable: __malloc_hook
The value of this variable is a pointer to the function that
malloc uses whenever it is called. You should define this
function to look like malloc; that is, like:
void *function (size_t size, const void *caller)
The value of caller is the return address found on the stack when
the malloc function was called. This value allows you to trace
the memory consumption of the program.
— Variable: __realloc_hook
The value of this variable is a pointer to function that realloc
uses whenever it is called. You should define this function to look
like realloc; that is, like:
The value of caller is the return address found on the stack when
the realloc function was called. This value allows you to trace the
memory consumption of the program.
— Variable: __free_hook
The value of this variable is a pointer to function that free
uses whenever it is called. You should define this function to look
like free; that is, like:
void function (void *ptr, const void *caller)
The value of caller is the return address found on the stack when
the free function was called. This value allows you to trace the
memory consumption of the program.
— Variable: __memalign_hook
The value of this variable is a pointer to function that memalign
uses whenever it is called. You should define this function to look
like memalign; that is, like:
The value of caller is the return address found on the stack when
the memalign function was called. This value allows you to trace the
memory consumption of the program.
You must make sure that the function you install as a hook for one of
these functions does not call that function recursively without restoring
the old value of the hook first! Otherwise, your program will get stuck
in an infinite recursion. Before calling the function recursively, one
should make sure to restore all the hooks to their previous value. When
coming back from the recursive call, all the hooks should be resaved
since a hook might modify itself.
— Variable: __malloc_initialize_hook
The value of this variable is a pointer to a function that is called
once when the malloc implementation is initialized. This is a weak
variable, so it can be overridden in the application with a definition
like the following:
An issue to look out for is the time at which the malloc hook functions
can be safely installed. If the hook functions call the malloc-related
functions recursively, it is necessary that malloc has already properly
initialized itself at the time when __malloc_hook etc. is
assigned to. On the other hand, if the hook functions provide a
complete malloc implementation of their own, it is vital that the hooks
are assigned to before the very first malloc call has
completed, because otherwise a chunk obtained from the ordinary,
un-hooked malloc may later be handed to __free_hook, for example.
In both cases, the problem can be solved by setting up the hooks from
within a user-defined function pointed to by
__malloc_initialize_hook—then the hooks will be set up safely
at the right time.
Here is an example showing how to use __malloc_hook and
__free_hook properly. It installs a function that prints out
information every time malloc or free is called. We just
assume here that realloc and memalign are not used in our
program.