如果您只想获取请求大量内存的位置,最简单的方法是修补malloc功能或创建一个具有malloc呼叫的新库并跟踪您的大小malloc function.我不是在谈论实现malloc调用.LD_PRELOAD这个库给你的应用程序.
这是一个示例代码:
/*
* gcc -shared -fPIC foo.c -ldl -Wl,-init,init_lib -o libfoo.so
*
* LD_PRELOAD this library in the environment of the target executable
*
*/
#include <stdio.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/errno.h>
#ifndef RTLD_NEXT
#define RTLD_NEXT ((void *)-1)
#endif
int init_lib(void)
{
return 0;
}
void *malloc(size_t size)
{
/* do required checks on size here */
return ((void* (*)(size_t))(dlsym(RTLD_NEXT, "malloc")))(size);
}
Run Code Online (Sandbox Code Playgroud)
您可以很好地修改此代码以执行其他一些操作.