- 论坛徽章:
- 1
|
本帖最后由 shihyu 于 2017-07-12 00:29 编辑
- #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函数里面 printf 函数会造成 crash ? 看網上dlsym hook一些例子只用 write or fprintf 而不用 printf |
|