LD_PRELOADing malloc和空闲 [英] LD_PRELOADing malloc and free
问题描述
我写了自己的 malloc 和 free ,并将它们编译到共享库中。我LD_PRELOAD库与我的程序。通过这种方式,我的程序总是使用我自定义的 malloc 和 free ,或者在某些情况下不是这样。我听说gcc已经建立了malloc和 free 。是否有可能我的gcc附带的glibc使用内置的 malloc 和 free 。第二,我注意到当我运行我的程序时,我看到 free 函数调用比 malloc / calloc 调用(98到16)。我自己不做任何内存分配(除了在一个地方),所以所有的分配都由我使用的标准库函数完成。还要注意我在我的程序中使用了pthread。如果你想知道,我的程序看起来像这样。
#include< pthread.h>
#include< stdio.h>
#include< stdlib.h>
#include< unistd.h>
#include< sys / mman.h>
#include< sys / types.h>
#define NUM_THREADS 8
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int sum;
float total = 1;
extern int __did_libc_start_main;
void * PrintHello(void * threadid)
{
long tid;
tid =(long)threadid;
pthread_mutex_lock(& m);
sum ++;
total * = total + tid * 0.097891313423578;
printf(p%d,tid%d,total =%g,start =%d!\\\
,getpid(),tid,total,0);
pthread_mutex_unlock(& m);
printf(Hello World!它是我,线程#%ld!\\\
,tid);
pthread_exit(NULL);
int main(int argc,char * argv [])
{
pthread_t threads [NUM_THREADS];
int rc;
long t;
char * p;
char * m;
fork();
p =(char *)mmap(0,4096,PROT_READ | PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS,-1,0);
p [0] = 78;
printf(p =%p,p [0] =%d,pid =%d!\\\
,p,p [0],getpid());
m =(char *)malloc(80);
printf(m =%p!\\\
,m); (t = 0; t {
printf(In main:创建线程%ld\\\
,t);
#if 1
;
rc = pthread_create(& threads [t],NULL,PrintHello,(void *)t);
if(rc){
printf(ERROR;从pthread_create()返回的代码是%d \ n,rc);
exit(-1); (t = 0; t pthread_join(threads [t],NULL);
}
}
printf(\\\
\\\
Total =%g\\\
\\\
,total);
$ b $ / * main()应该做的最后一件事* /
pthread_exit(NULL);
#endif
printf(\\\
\\\
%d:完成没有重大问题\\\
\\\
,getpid());
返回0;
LD_PRELOAD
来覆盖 malloc
等等。这是如何 DUMA 有效。 除<$ code> malloc , calloc
和 free
,请确保覆盖 realloc
, memalign
和 valloc
。另外,你可能需要重写C ++ new
, new []
, delete
和 delete []
。
请参阅使用LD_PRELOAD机制重写'malloc'以获取如何执行此操作的示例。
I wrote my own malloc and free and compiled them in a shared library. I LD_PRELOAD that library with my program. In this way would my program always use my custom malloc and free or are there cases where it is not so. I've heard that gcc has built in malloc and free too. Is it possible that the glibc that came with my gcc is using the builtin malloc and free.
Secondly, I notice that when I run my program, I'm seeing the free function call more often than the malloc/calloc calls (98 to 16). I don't do any memory allocation myself (except in one place), so all allocation is being done by standard library functions that I use. And also note I'm using pthread in my program. If you want to know, my program looks like this.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#define NUM_THREADS 8
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int sum;
float total = 1;
extern int __did_libc_start_main;
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
pthread_mutex_lock( &m );
sum++;
total *= total + tid * 0.097891313423578;
printf( "p%d, tid%d, total = %g, start = %d!\n", getpid(), tid, total, 0 );
pthread_mutex_unlock( &m );
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
char * p;
char * m;
fork();
p = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
p[0] = 78;
printf( "p = %p, p[0] = %d, pid = %d!\n", p, p[0], getpid() );
m = (char*)malloc( 80 );
printf( "m = %p!\n", m );
#if 1
for(t=0; t<NUM_THREADS; t++)
{
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0; t<NUM_THREADS; t++)
pthread_join(threads[t], NULL);
printf( "\n\nTotal = %g\n\n", total );
/* Last thing that main() should do */
pthread_exit(NULL);
#endif
printf( "\n\n%d: Done without major problems\n\n", getpid() );
return 0;
}
Using LD_PRELOAD
to override malloc
etc. is expected to work; this is how e.g. DUMA works.
In addition to malloc
, calloc
and free
, make sure you override realloc
, memalign
and valloc
. In addition you might need to override C++ new
, new[]
, delete
and delete[]
.
See Overriding 'malloc' using the LD_PRELOAD mechanism for an example of how to do this right.
这篇关于LD_PRELOADing malloc和空闲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!