#include #include #include #include #include #include #include #include class MAP__LOCK{ char line[40000]; char *pt; public: MAP__LOCK(){ pt = line; timestamp(); } ~MAP__LOCK(){ endline(); } void printf (const char *ctl, ...); void timestamp(); void endline(); }; void MAP__LOCK::printf (const char *ctl, ...) { int maxlen = sizeof(line)-20-(pt-line); if (maxlen > 0){ va_list list; va_start (list,ctl); int len = vsnprintf (pt,maxlen,ctl,list); // printf ("len=%d maxlen=%d\n",len,maxlen); if (len >= maxlen){ pt += maxlen-1; }else{ pt += len; } va_end (list); } } void MAP__LOCK::timestamp() { struct timeval tv; if (gettimeofday(&tv,NULL)!=-1){ printf ("%08lx.%08lx ",tv.tv_sec,tv.tv_usec); } } static int fout=-1; void MAP__LOCK::endline() { *pt++ = '\n'; write (fout,line,(int)(pt-line)); pt = line; } extern "C" void *__libc_malloc(unsigned int size); extern "C" void *__libc_calloc(unsigned int n, unsigned int size); extern "C" void *__libc_free(void *pt); extern "C" void *__libc_realloc(void *pt, unsigned int size); extern "C" pid_t __libc_fork(); void show_trace_stub(); void show_trace(MAP__LOCK &); static bool init = false; static void show_init(const char *insert) { // MAP__LOCK l; if (fout == -1){ init = true; static bool readpipe = false; char buf[PATH_MAX]; buf[0] = '\0'; if (!readpipe){ readpipe = true; int len = read (976,buf,sizeof(buf)-1); if (len > 0){ buf[len] = '\0'; } close (976); } const char *prefix = buf; if (buf[0] == '\0'){ prefix = getenv ("MAPMALLOCPREFIX"); if (prefix == NULL) prefix = "/tmp/mapmalloc"; } char path[PATH_MAX]; snprintf (path,sizeof(path)-1,"%s%s-%d.log",prefix,insert,getpid()); fout = open64 (path,O_WRONLY|O_CREAT|O_APPEND,0644); init = false; } } static void show_init () { show_init (""); } void show_trace0(MAP__LOCK &l) { unsigned long data=10; unsigned long *stack = &data; int i; for (i=0; i<1000; i++){ // printf ("stack=%08x %08x %08x %08x\n",stack,*stack,show_trace,main); if (*stack >= (unsigned long)show_trace && *stack < (unsigned long)show_trace_stub) break; stack++; } if (i == 1000){ l.printf ("Il ne trouve pas le stack"); }else{ //printf ("trouve %08x %08x %08x %08x\n" // ,stack[-1],stack[0],stack[1],stack[2]); stack--; stack = (unsigned long*)stack[0]; stack = (unsigned long*)stack[0]; while (stack != NULL && stack >= (void*)0x1000){ // printf ("bt: %08x %08x\n",stack[0],stack[1]); l.printf (" %08lx",stack[1]); stack = (unsigned long*)stack[0]; } } } void show_trace(MAP__LOCK &l) { show_trace0(l); } void show_trace_stub(){} extern "C" void *malloc (size_t size) { void *ret = NULL; if (size != 0){ ret = __libc_malloc(size); if (!init){ MAP__LOCK l; show_init(); l.printf ("malloc %08lx %d: ",(long)ret,size); show_trace(l); } } // fprintf (stderr,"bt %p %p %p\n",show_trace0,show_trace,malloc); return ret; } extern "C" void free(void *p) { if (p != NULL){ MAP__LOCK l; l.printf ("free %08lx:",(long)p); show_trace(l); __libc_free (p); } } extern "C" void *realloc (void *pt, size_t size) { void *ret = __libc_realloc (pt,size); MAP__LOCK l; l.printf ("realloc %08lx %08lx %u:",pt,ret,size); show_trace(l); return ret; } extern "C" void *calloc (size_t n, size_t elm) { void *ret = __libc_calloc(n,elm); if (!init){ MAP__LOCK l; show_init(); l.printf ("calloc %08lx %d: ",(long)ret,n*elm); show_trace(l); } return ret; } extern "C" char *strdup (const char *s) { char *ret = NULL; if (s != NULL){ int len = strlen(s)+1; ret = (char*)__libc_malloc (len); if (ret != NULL) strcpy (ret,s); if (!init){ MAP__LOCK l; show_init(); l.printf ("strdup %08x %d: ",(long)ret,len); show_trace(l); } } return ret; } extern "C" pid_t fork() { pid_t old = getpid(); pid_t ret = __libc_fork(); if (ret == 0){ close (fout); fout = -1; char tmp[20]; snprintf (tmp,sizeof(tmp)-1,"-%d-fork",old); show_init(tmp); } return ret; } #if 0 extern "C" ssize_t __libc_write (int fd, const void *buf, size_t len); extern "C" ssize_t write (int fd, const void *buf, size_t len) { int ret = -1; if (fd == 976000000){ MAP__LOCK l; show_init(); char *start = (char*)buf; char *pt = strchr(start,':'); if (pt == NULL){ l.printf ("appmsg NULL :"); }else{ *pt++ = '\0'; l.printf ("appmsg %s ",(char*)buf); char *end = start+len; while (pt < end) l.printf ("%02x",*pt++); l.printf (": "); } show_trace(l); ret = 0; }else{ ret = __libc_write (fd,buf,len); } return ret; } #endif