使用 dlsym 跟踪 mmap
我正在尝试测量某些程序映射需要多少内存。 我使用以下代码:
void * mmap (void * addr, size_t len, int prot, int flags, int fildes, off_t off) {
printf("in mmap1\n");
static void *(*realfn)(void*, size_t, int, int, int, off_t)
= (void *(*)(void*, size_t, int, int, int, off_t))dlsym(RTLD_NEXT, "mmap");
printf("in mmap2\n");
void * result = (*realfn)(addr, len, prot, flags, fildes, off);
if ((int) result != -1) {
stats.add (len);
}
return result;
}
stats 是保存统计信息的一些全局变量。 我将此代码转换为共享对象并与其链接。对于某些程序来说它可以工作,但是对于 有些程序,在mmap1中打印一次,然后在mmap1中再次打印,然后什么也没有 发生了(程序卡在这一点上,直到我杀死它,永远不会到达 mmap2。
我读到了有关 dlsym 的内容,但我找不到导致此问题的问题。 我很乐意从更有经验的人那里来。 谢谢。
I am trying to measure how much memory does some program mmaps.
I am using the following code:
void * mmap (void * addr, size_t len, int prot, int flags, int fildes, off_t off) {
printf("in mmap1\n");
static void *(*realfn)(void*, size_t, int, int, int, off_t)
= (void *(*)(void*, size_t, int, int, int, off_t))dlsym(RTLD_NEXT, "mmap");
printf("in mmap2\n");
void * result = (*realfn)(addr, len, prot, flags, fildes, off);
if ((int) result != -1) {
stats.add (len);
}
return result;
}
stats is some global variable that saves the stats.
I turn this code into a shared object and link with it. For some programs it works, but for
some programs, in mmap1 is printed once, then in mmap1 is printed again, and then nothing
happens (the program gets stuck at this point until I kill it, never reaching in mmap2.
I read about dlsym, but I can't find the problem causing this.
I would love to here from someone more experienced.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论