diff options
Diffstat (limited to 'c/malloc_hook.c')
-rw-r--r-- | c/malloc_hook.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/c/malloc_hook.c b/c/malloc_hook.c new file mode 100644 index 0000000..aa22379 --- /dev/null +++ b/c/malloc_hook.c @@ -0,0 +1,37 @@ +#include <stdlib.h> +#include <stdio.h> + +#define __USE_GNU +#include <dlfcn.h> + +typedef void* (*real_malloc_t) (size_t); + +static real_malloc_t real_malloc = NULL; + +static void __mtrace_init(void) +{ + real_malloc = (real_malloc_t) dlsym(RTLD_NEXT, "malloc"); + if (NULL == real_malloc) { + fprintf(stderr, "Error in `dlsym`: %s\n", dlerror()); + return; + } +} + +void *malloc(size_t size) +{ + if(real_malloc == NULL) + __mtrace_init(); + + void *p = NULL; + fprintf(stderr, "malloc(%zu) = ", size); + p = real_malloc(size); + fprintf(stderr, "%p\n", p); + return p; +} + + +int main(int argc, char **argv, char **env) +{ + void * nothing = malloc(666); + free(nothing); +} |