Following up from Wrapping LibC Functions a couple of months ago, here's how you can build those functions into a dynamically loaded library.
The code to log calloc() and free() looks something like:
#include <stdlib.h>
void *calloc (size_t count, size_t size)
{
void *ptr = (void *) __libc_calloc(count, size);
DEBUG("calloc(%d, %d) = %p\n", count, size, ptr);
return ptr;
}
void free (void * ptr)
{
__libc_free(ptr);
DEBUG("free(%p)\n",ptr);
}
If the file's called test_alloc.c build the library with:
$ gcc -shared -ldl -o alloc.so test_alloc.c
Then run a program with these intercept functions:
$ LD_PRELOAD=alloc.so ./program