I'm on Linux, and trying to work with the examples Create a wrapper function for malloc and free in C; but it seems I'm not understanding something.
I have one .c source representing the .so file; and another .c source which is a tester (below). So I build like so:
# the .so
gcc -c -fpic mymalloc.c
gcc -shared -Wl,-soname,libmymalloc.so -o libmymalloc.so mymalloc.o
# the tester
gcc -o malloctest -Wall -g malloctest.c
... and finally I test like so:
$ LD_PRELOAD=./libmymalloc.so ./malloctest
malloc'ed 5 arrays
free'd 5 arrays
... and I just get the test program output - not printouts from the .so on each malloc/free call (as I, otherwise, understood the effect should be).
Can anyone help me on where am I going wrong?
Many thanks in advance,
Cheers!
mymalloc.c:
//~ gcc -c -fpic mymalloc.c
//~ gcc -shared -Wl,-soname,libmymalloc.so -o libmymalloc.so mymalloc.o
//~ https://svn.apache.org/repos/asf/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h
//~ https://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
void * debug_malloc( size_t len, char * file, int line);
void debug_free( void * addr, char * file, int line );
//~ #define mymalloc(x) debug_malloc(x,__FILE__,__LINE__)
//~ #define myfree(x) debug_free(x,__FILE__,__LINE__)
#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)
//~ void* malloc(size_t sz)
void * debug_malloc( size_t len, char * file, int line )
{
void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");
//~ printf("malloc\n");
printf("Malloc from %s:%d",file,line);
return libc_malloc(len);
}
//~ void free(void *p)
void debug_free( void * addr, char * file, int line )
{
void (*libc_free)(void*) = dlsym(RTLD_NEXT, "free");
//~ printf("free\n");
printf("Free from %s:%d",file,line);
libc_free(addr);
}
//~ int main()
//~ {
//~ free(malloc(10));
//~ return 0;
//~ }
malloctest.c:
// gcc -o malloctest -Wall -g malloctest.c
#include <stdlib.h>
#include <stdio.h>
int main() {
int *ptr1 = (int *) malloc(10 * sizeof (int));
int *ptr2 = (int *) malloc(10 * sizeof (int));
int *ptr3 = (int *) malloc(10 * sizeof (int));
int *ptr4 = (int *) malloc(10 * sizeof (int));
int *ptr5 = (int *) malloc(10 * sizeof (int));
printf("malloc'ed 5 arrays\n");
free(ptr1);
free(ptr2);
free(ptr3);
free(ptr4);
free(ptr5);
printf("free'd 5 arrays\n");
return 0;
}