I’m trying to write a simple shared library that would log malloc calls to stderr (a sort of ‘mtrace’ if you will).
However, this is not working.
Here’s what I do:
/* mtrace.c */
#include <dlfcn.h>
#include <stdio.h>
static void* (*real_malloc)(size_t);
void *malloc(size_t size)
{
void *p = NULL;
fprintf(stderr, "malloc(%d) = ", size);
p = real_malloc(size);
fprintf(stderr, "%p\n", p);
return p;
}
static void __mtrace_init(void) __attribute__((constructor));
static void __mtrace_init(void)
{
void *handle = NULL;
handle = dlopen("libc.so.6", RTLD_LAZY);
if (NULL == handle) {
fprintf(stderr, "Error in `dlopen`: %s\n", dlerror());
return;
}
real_malloc = dlsym(handle, "malloc");
if (NULL == real_malloc) {
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
return;
}
}
I compile this with:
gcc -shared -fPIC -o mtrace.so mtrace.c
And then when I try to execute ls:
$ LD_PRELOAD=./mtrace.so ls
malloc(352) = Segmentation fault
Now, I suspect that dlopen needs malloc, and as I am redefining it within the shared library, it uses that version with the still unassigned real_malloc.
The question is…how do I make it work?
P.S. sorry for the paucity in tags, I couldn’t find appropriate tags, and I still don’t have enough reputation to create new ones.
I always do it this way:
Don’t use constructors, just initialize at first call to
malloc. UseRTLD_NEXTto avoiddlopen. You can also try malloc hooks. Be aware that all those are GNU extensions, and probably wont work elsewhere.