了解在C中为malloc和free创建包装器函数

了解在C中为malloc和free创建包装器函数,c,C,我使用Linux,并尝试使用这些示例;但我似乎不明白什么 我有一个代表.so文件的.c源代码;和另一个.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=.

我使用Linux,并尝试使用这些示例;但我似乎不明白什么

我有一个代表.so文件的.c源代码;和另一个.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
。。。最后,我这样测试:

# 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
。。。我只是得到测试程序的输出,而不是来自.so的打印输出,在每次malloc/free调用中(否则,我理解效果应该是这样的)

谁能帮我弄清楚我哪里出了问题? 非常感谢,
干杯

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;
//~ }
/~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
#定义GNU源
#包括
#包括
void*debug\u malloc(大小长度,字符*文件,整数行);
void debug_free(void*addr,char*file,int行);
//~#定义mymalloc(x)调试malloc(x,_文件_,_行_)
//~#定义myfree(x)调试(x、_u文件、_u行uu)
#定义malloc(x)调试
#定义空闲(x)调试空闲(x,_u文件,_uu行)
//~void*malloc(尺码)
void*debug\u malloc(大小长度,字符*文件,整数行)
{
void*(*libc_malloc)(size_t)=dlsym(RTLD_NEXT,malloc);
//~printf(“malloc\n”);
printf(“来自%s:%d的Malloc”,文件,行);
返回libc_malloc(len);
}
//~void free(void*p)
void debug_free(void*addr、char*file、int行)
{
void(*libc_free)(void*)=dlsym(RTLD_NEXT,“free”);
//~printf(“免费”);
printf(“从%s中释放:%d”,文件,行);
免费图书馆(addr);
}
//~int main()
//~ {
//~free(malloc(10));
//~0;
//~ }
malloctest.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;
}
// 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;
}
//gcc-o malloctest-Wall-g malloctest.c
#包括
#包括
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数组\n”);
免费(ptr1);
免费(ptr2);
免费(ptr3);
免费(ptr4);
免费(ptr5);
printf(“自由的5个数组\n”);
返回0;
}

您实际上调用了libc malloc,而不是您的实现。试着用debug\u malloc替换malloc,或者用debug\u free替换free,看看您实际调用libc malloc而不是实现的区别。尝试将malloc替换为debug\u malloc或将free替换为debug\u free,并查看其区别

定义需要位于头文件中,该头文件包含在malloctest.c文件中,这样将调用正确的malloc,free。由于define只在mymalloc中工作,所以现在没有效果

只需要做一个标题

#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)

加上原型并包含在malloctest.c中

定义需要包含在头文件中,这样就可以调用正确的malloc,free。由于define只在mymalloc中工作,所以现在没有效果

只需要做一个标题

#define malloc(x) debug_malloc(x,__FILE__,__LINE__)
#define free(x) debug_free(x,__FILE__,__LINE__)

加上原型并包含在malloctest.c中

由于OP已经有点密集,我将在这里进行澄清:

我对LD_PRELOAD方法感兴趣,因为我认为可以使用它,而不必更改原始可执行文件(这对我来说还包括不将其链接到新的.so)。我试着按照@Abhajit和@andersk的建议,把所有的东西都放在可执行文件中——这会消除。所以,只是想看看我是否能让它工作。。。我不能

当我尝试构建以下源代码时,我得到:

/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
。。。如果使用
-fPIC
构建,则不会出现
dlsym
问题,而AFAIK仅用于.so文件。因此,使用
dlsym
的代码最初一定是打算在.So中使用的,但是如何编译整个代码,使其工作

修改的单文件malloctest.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;
}
// 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;
}
//gcc-o malloctest-Wall-g malloctest.c
//_GNU_SOURCE=>RTLD_NEXT
#定义GNU源
#包括
#包括
#包括
//~#包括“malloctest.h”
void*debug\u malloc(大小长度,字符*文件,整数行)
{
void*(*libc_malloc)(size_t)=dlsym(RTLD_NEXT,malloc);
//~printf(“malloc\n”);
printf(“来自%s:%d的Malloc”,文件,行);
返回libc_malloc(len);
}
//~void free(void*p)
void debug_free(void*addr、char*file、int行)
{
void(*libc_free)(void*)=dlsym(RTLD_NEXT,“free”);
//~printf(“免费”);
printf(“从%s中释放:%d”,文件,行);
免费图书馆(addr);
}
#定义malloc(x)调试
#定义空闲(x)调试空闲(x,_u文件,_uu行)
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数组\n”);
免费(ptr1);
免费(ptr2);
免费(ptr3);
免费(ptr4);
免费(ptr5);
printf(“自由的5个数组\n”);
返回0;
}

由于OP已经有点密集,我将在这里进行澄清:

我对LD_PRELOAD方法感兴趣,因为我认为可以使用它,而不必更改原始可执行文件(这对我来说还包括不将其链接到新的.so)。我试着按照@Abhajit和@andersk的建议,把所有的东西都放在可执行文件中——这会消除。所以,只是想看看我是否能让它工作。。。我不能

当我尝试构建以下源代码时,我得到:

/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
。。。如果使用
-fPIC
构建,则不会出现
dlsym
问题,而AFAIK仅用于.so文件。因此,使用
dlsym
的代码最初一定是打算在.So中使用的,但是一个com