我在Linux上,并试图使用示例Create a wrapper function for malloc and free in C;但我似乎没有理解什么。
我有一个.c源代码表示.so文件,另一个.c源代码是一个测试器(如下所示)。所以我建得像这样:
# the .so
gcc -c -fpic mymalloc.c
gcc -shared -Wl,-soname,libmymalloc.so -o libmymalloc.so mymalloc.o
# the tester
gcc -o malloctest -Wall -g malloctest.c..。最后,我像这样测试:
$ LD_PRELOAD=./libmymalloc.so ./malloctest
malloc'ed 5 arrays
free'd 5 arrays..。而且我只是得到测试程序的输出--而不是在每个malloc/free调用上从.so输出(否则,我理解效果应该是这样)。
有人能帮我弄清楚我哪里出了问题吗?事先非常感谢,
干杯!
mymalloc.c:
//~ gcc -c -fpic mymalloc.c
//~ gcc -shared -Wl,-soname,libmymalloc.so -o libmymalloc.so mymalloc.o
//~ https://svn.apache.org/repos/asf/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h
//~ https://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
void * debug_malloc( size_t len, char * file, int line);
void debug_free( void * addr, char * file, int line );
//~ #define mymalloc(x) debug_malloc(x,__FILE__,__LINE__)
//~ #define myfree(x) debug_free(x,__FILE__,__LINE__)
#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)
//~ void* malloc(size_t sz)
void * debug_malloc( size_t len, char * file, int line )
{
void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");
//~ printf("malloc\n");
printf("Malloc from %s:%d",file,line);
return libc_malloc(len);
}
//~ void free(void *p)
void debug_free( void * addr, char * file, int line )
{
void (*libc_free)(void*) = dlsym(RTLD_NEXT, "free");
//~ printf("free\n");
printf("Free from %s:%d",file,line);
libc_free(addr);
}
//~ int main()
//~ {
//~ free(malloc(10));
//~ return 0;
//~ }C.
// gcc -o malloctest -Wall -g malloctest.c
#include <stdlib.h>
#include <stdio.h>
int main() {
int *ptr1 = (int *) malloc(10 * sizeof (int));
int *ptr2 = (int *) malloc(10 * sizeof (int));
int *ptr3 = (int *) malloc(10 * sizeof (int));
int *ptr4 = (int *) malloc(10 * sizeof (int));
int *ptr5 = (int *) malloc(10 * sizeof (int));
printf("malloc'ed 5 arrays\n");
free(ptr1);
free(ptr2);
free(ptr3);
free(ptr4);
free(ptr5);
printf("free'd 5 arrays\n");
return 0;
}发布于 2011-11-29 05:26:54
实际上,您已经调用了libc,而不是您的实现。尝试用debug_malloc替换malloc,或者用debug_free替换debug_free,看看不同之处。
发布于 2011-11-29 05:31:08
define需要包含在malloctest.c文件中的头文件中,这样就可以调用正确的malloc了。因为现在没有任何效果,因为定义只在mymalloc中工作。
只需使用
#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)加上原型并包含在malloctest.c中。
发布于 2011-11-29 06:07:22
由于OP已经比较密集,我将在这里澄清如下:
我对LD_PRELOAD方法很感兴趣,因为我认为可以使用它,而不需要更改原始的可执行文件(对我来说,这也包括不将它链接到新的.so)。我试着遵循@Abhajit和@Anders的建议,实际上把所有东西都放在可执行文件中--这将消除.so,但只是为了看看我是否能让它工作.但我做不到。
当我试图构建下面的源代码时,我得到:
/tmp/ccyUrbW8.o: In function `debug_malloc':
/path/to/malloctest.c:13: undefined reference to `dlsym'
/tmp/ccyUrbW8.o: In function `debug_free':
/path/to/malloctest.c:22: undefined reference to `dlsym'
collect2: ld returned 1 exit status..。如果使用dlsym构建-fPIC,则不会出现.so问题,AFAIK仅用于.so文件。所以使用dlsym的代码一定是最初打算在.so中使用的--但是如何编译整个程序,所以它是如何工作的呢?
修改后的单一文件malloctest.c:
// gcc -o malloctest -Wall -g malloctest.c
// _GNU_SOURCE => RTLD_NEXT
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
//~ #include "malloctest.h"
void * debug_malloc( size_t len, char * file, int line )
{
void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");
//~ printf("malloc\n");
printf("Malloc from %s:%d",file,line);
return libc_malloc(len);
}
//~ void free(void *p)
void debug_free( void * addr, char * file, int line )
{
void (*libc_free)(void*) = dlsym(RTLD_NEXT, "free");
//~ printf("free\n");
printf("Free from %s:%d",file,line);
libc_free(addr);
}
#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)
int main() {
int *ptr1 = (int *) malloc(10 * sizeof (int));
int *ptr2 = (int *) malloc(10 * sizeof (int));
int *ptr3 = (int *) malloc(10 * sizeof (int));
int *ptr4 = (int *) malloc(10 * sizeof (int));
int *ptr5 = (int *) malloc(10 * sizeof (int));
printf("malloc'ed 5 arrays\n");
free(ptr1);
free(ptr2);
free(ptr3);
free(ptr4);
free(ptr5);
printf("free'd 5 arrays\n");
return 0;
}https://stackoverflow.com/questions/8306231
复制相似问题