#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <dlfcn.h>
void* malloc(size_t size)
{
char buf[32];
static void* (*real_malloc)(size_t) = NULL;
printf("ggggggggggg");
if (real_malloc == NULL) {
*(void**)(&real_malloc) = dlsym(RTLD_NEXT, "malloc");
}
sprintf(buf, "malloc called, size = %zu\n", size);
write(2, buf, strlen(buf));
return real_malloc(size);
}
void free(void* ptr)
{
char buf[32];
static void* (*real_free)(void* ptr) = NULL;
if (real_free == NULL) {
*(void**)(&real_free) = dlsym(RTLD_NEXT, "free");
}
sprintf(buf, "free called, ptr address = %p\n", ptr);
write(2, buf, strlen(buf));
real_free(ptr);
}
gcc -D_GNU_SOURCE -shared -fPIC -o libmcount.so malloc_count.c -ldl
LD_PRELOAD=./libmcount.so ls
那悶在malloc函數裡面 p
rintf 函數會造成 crash ? 看網路上dlsym hook一些例子只用 write 而不用 printf