summaryrefslogtreecommitdiffstats
path: root/c/malloc_hook.c
blob: aa22379738a44fb43afd668f0ed4aeb74b91b7dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
}