--- glibc-2.10.1-original/malloc/malloc.c	2009-04-18 10:23:30.000000000 +0200
+++ glibc-2.10.1/malloc/malloc.c	2010-06-25 18:57:13.577331845 +0200
@@ -2337,6 +2337,24 @@
    ----------- Internal state representation and initialization -----------
 */
 
+int trace_file = -1;
+mutex_t trace_file_mutex;
+__thread int flag_trace = 0;
+
+static void __write_alloC(void * buf, unsigned int size) {
+	char * buffer = buf;
+	unsigned int bw = 0, bf = 0;
+	do {
+		bf = write(trace_file, buffer + bw, size - bw);
+		if (bf == -1) {
+			close(trace_file);
+			trace_file = -1;
+		} else
+			bw += bf;
+	} while(bw < size && trace_file != -1);
+}
+
+
 struct malloc_state {
   /* Serialize access.  */
   mutex_t mutex;
@@ -3593,11 +3611,40 @@
 {
   mstate ar_ptr;
   Void_t *victim;
+  
+  /* Copy param1 (preserve original value for tracing) */
+  size_t param_1 = bytes;
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
 
   __malloc_ptr_t (*hook) (size_t, __const __malloc_ptr_t)
     = force_reg (__malloc_hook);
-  if (__builtin_expect (hook != NULL, 0))
-    return (*hook)(bytes, RETURN_ADDRESS (0));
+  if (__builtin_expect (hook != NULL, 0)) {
+
+	void * victim = (*hook)(bytes, RETURN_ADDRESS (0));
+
+	/* Trace malloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+		unsigned int alloc = 1; /* Allocation header */
+		__write_alloC(&alloc, sizeof(alloc));
+		__write_alloC(&param_1, sizeof(param_1));
+		__write_alloC(&victim, sizeof(victim));
+		flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+
+	return victim;
+
+  }
+
 
   arena_lookup(ar_ptr);
 #if 0
@@ -3659,6 +3706,21 @@
     (void)mutex_unlock(&ar_ptr->mutex);
   assert(!victim || chunk_is_mmapped(mem2chunk(victim)) ||
 	 ar_ptr == arena_for_chunk(mem2chunk(victim)));
+
+  /* Trace malloc() */
+  (void) mutex_lock(&trace_file_mutex);
+  if (trace_file != -1 && !already_tracing) {
+
+        unsigned int alloc = 1; /* Allocation header */
+        __write_alloC(&alloc, sizeof(alloc));
+        __write_alloC(&param_1, sizeof(param_1));
+        __write_alloC(&victim, sizeof(victim));
+        flag_trace = 0;
+
+  }
+  (void) mutex_unlock(&trace_file_mutex);
+  /* end tracing */
+  
   return victim;
 }
 #ifdef libc_hidden_def
@@ -3670,6 +3732,27 @@
 {
   mstate ar_ptr;
   mchunkptr p;                          /* chunk corresponding to mem */
+  /* Preserve function's params (for tracing) */
+  void * param_1 = mem;
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
+
+  /* Trace free() */
+  (void) mutex_lock(&trace_file_mutex);
+  if (trace_file != -1 && !already_tracing) {
+
+        unsigned int alloc = 2; /* Allocation header */
+        __write_alloC(&alloc, sizeof(alloc));
+        __write_alloC(&param_1, sizeof(param_1));
+        flag_trace = 0;
+
+  }
+  (void) mutex_unlock(&trace_file_mutex);
+  /* end tracing */
 
   void (*hook) (__malloc_ptr_t, __const __malloc_ptr_t)
     = force_reg (__free_hook);
@@ -3729,17 +3812,89 @@
 
   Void_t* newp;             /* chunk to return */
 
+  /* Preserve function's params (for tracing) */
+  void * param_1 = oldmem;
+  size_t param_2 = bytes;
+  
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
+
   __malloc_ptr_t (*hook) (__malloc_ptr_t, size_t, __const __malloc_ptr_t) =
     force_reg (__realloc_hook);
-  if (__builtin_expect (hook != NULL, 0))
-    return (*hook)(oldmem, bytes, RETURN_ADDRESS (0));
+  if (__builtin_expect (hook != NULL, 0)) {
+
+	void * victim_ret = (*hook)(oldmem, bytes, RETURN_ADDRESS (0));
+	
+	/* Trace realloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 3; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&victim_ret, sizeof(victim_ret));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+	
+	return victim_ret;
+  }
 
 #if REALLOC_ZERO_BYTES_FREES
-  if (bytes == 0 && oldmem != NULL) { public_fREe(oldmem); return 0; }
+  if (bytes == 0 && oldmem != NULL) {
+
+	public_fREe(oldmem);
+	void * victim_ret = 0;
+	
+	/* Trace realloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 3; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&victim_ret, sizeof(victim_ret));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+	
+	return victim_ret;
+  }
 #endif
 
   /* realloc of null is supposed to be same as malloc */
-  if (oldmem == 0) return public_mALLOc(bytes);
+  if (oldmem == 0) {
+
+	void * victim_ret = public_mALLOc(bytes);
+	
+	/* Trace realloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 3; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&victim_ret, sizeof(victim_ret));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+	
+	return victim_ret;
+	
+  }
 
   /* chunk corresponding to oldmem */
   const mchunkptr oldp    = mem2chunk(oldmem);
@@ -3766,12 +3921,69 @@
 
 #if HAVE_MREMAP
     newp = mremap_chunk(oldp, nb);
-    if(newp) return chunk2mem(newp);
+    if(newp) {
+		
+        void * victim_ret = chunk2mem(newp);
+
+        /* Trace realloc() */
+		(void) mutex_lock(&trace_file_mutex);
+		if (trace_file != -1 && !already_tracing) {
+
+				unsigned int alloc = 3; /* Allocation header */
+				__write_alloC(&alloc, sizeof(alloc));
+				__write_alloC(&param_1, sizeof(param_1));
+				__write_alloC(&param_2, sizeof(param_2));
+				__write_alloC(&victim_ret, sizeof(victim_ret));
+				flag_trace = 0;
+
+		}
+		(void) mutex_unlock(&trace_file_mutex);
+		/* end tracing */
+        
+        return victim_ret;
+    }
 #endif
     /* Note the extra SIZE_SZ overhead. */
-    if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
+    if(oldsize - SIZE_SZ >= nb) {
+
+        void * victim_ret = oldmem;
+        
+        /* Trace realloc() */
+		(void) mutex_lock(&trace_file_mutex);
+		if (trace_file != -1 && !already_tracing) {
+
+				unsigned int alloc = 3; /* Allocation header */
+				__write_alloC(&alloc, sizeof(alloc));
+				__write_alloC(&param_1, sizeof(param_1));
+				__write_alloC(&param_2, sizeof(param_2));
+				__write_alloC(&victim_ret, sizeof(victim_ret));
+				flag_trace = 0;
+
+		}
+		(void) mutex_unlock(&trace_file_mutex);
+		/* end tracing */
+        
+        return oldmem; /* do nothing */
+    }
     /* Must alloc, copy, free. */
     newmem = public_mALLOc(bytes);
+    void * victim_ret = newmem;
+
+    /* Trace realloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 3; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&victim_ret, sizeof(victim_ret));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+    
     if (newmem == 0) return 0; /* propagate failure */
     MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
     munmap_chunk(oldp);
@@ -3828,6 +4040,23 @@
 	}
     }
 
+  void * victim_ret = newp;
+
+	/* Trace realloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 3; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&victim_ret, sizeof(victim_ret));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+
   return newp;
 }
 #ifdef libc_hidden_def
@@ -3840,14 +4069,64 @@
   mstate ar_ptr;
   Void_t *p;
 
+  /* Preserve  function's params (for tracing) */
+  size_t param_1 = alignment;
+  size_t param_2 = bytes;
+
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
+
   __malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, size_t,
 					__const __malloc_ptr_t)) =
     force_reg (__memalign_hook);
-  if (__builtin_expect (hook != NULL, 0))
-    return (*hook)(alignment, bytes, RETURN_ADDRESS (0));
+  if (__builtin_expect (hook != NULL, 0)) {
+	void * p = (*hook)(alignment, bytes, RETURN_ADDRESS (0));
+	  
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 6; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&p, sizeof(p));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+	
+    return p;
+    
+  }
+
 
   /* If need less alignment than we give anyway, just relay to malloc */
-  if (alignment <= MALLOC_ALIGNMENT) return public_mALLOc(bytes);
+  if (alignment <= MALLOC_ALIGNMENT) {
+	void * p = public_mALLOc(bytes);
+
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 6; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&p, sizeof(p));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+	
+    return p;
+  }
 
   /* Otherwise, ensure that it is at least a minimum chunk size */
   if (alignment <  MINSIZE) alignment = MINSIZE;
@@ -3880,6 +4159,23 @@
     (void)mutex_unlock(&ar_ptr->mutex);
   assert(!p || chunk_is_mmapped(mem2chunk(p)) ||
 	 ar_ptr == arena_for_chunk(mem2chunk(p)));
+
+
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 6; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&p, sizeof(p));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+  
   return p;
 }
 #ifdef libc_hidden_def
@@ -3891,6 +4187,15 @@
 {
   mstate ar_ptr;
   Void_t *p;
+  /* Preserve function's params (for tracing) */
+  size_t param_1 = bytes;
+
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
 
   if(__malloc_initialized < 0)
     ptmalloc_init ();
@@ -3900,8 +4205,26 @@
   __malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, size_t,
 					__const __malloc_ptr_t)) =
     force_reg (__memalign_hook);
-  if (__builtin_expect (hook != NULL, 0))
-    return (*hook)(pagesz, bytes, RETURN_ADDRESS (0));
+  if (__builtin_expect (hook != NULL, 0)) {
+	  
+    void * p = (*hook)(pagesz, bytes, RETURN_ADDRESS (0));
+
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 5; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&p, sizeof(p));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+
+    return p;
+  }
 
   arena_get(ar_ptr, bytes + pagesz + MINSIZE);
   if(!ar_ptr)
@@ -3928,6 +4251,20 @@
   assert(!p || chunk_is_mmapped(mem2chunk(p)) ||
 	 ar_ptr == arena_for_chunk(mem2chunk(p)));
 
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 5; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&p, sizeof(p));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+
   return p;
 }
 
@@ -3988,6 +4325,10 @@
   unsigned long nclears;
   INTERNAL_SIZE_T* d;
 
+  /* Preserve function's params (for tracing) */
+  size_t param_1 = n;
+  size_t param_2 = elem_size;
+
   /* size_t is unsigned so the behavior on overflow is defined.  */
   bytes = n * elem_size;
 #define HALF_INTERNAL_SIZE_T \
@@ -3999,11 +4340,34 @@
     }
   }
 
+  /* Inhibit nested tracing */
+  int already_tracing = 0;
+  if(flag_trace)
+    already_tracing = 1;
+  else
+    flag_trace = 1;
+
   __malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, __const __malloc_ptr_t)) =
     force_reg (__malloc_hook);
   if (__builtin_expect (hook != NULL, 0)) {
     sz = bytes;
     mem = (*hook)(sz, RETURN_ADDRESS (0));
+
+	/* Trace memallign() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 4; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&mem, sizeof(mem));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+    
     if(mem == 0)
       return 0;
 #ifdef HAVE_MEMCPY
@@ -4074,6 +4438,22 @@
     {
       if (__builtin_expect (perturb_byte, 0))
 	MALLOC_ZERO (mem, sz);
+
+		/* Trace memallign() */
+		(void) mutex_lock(&trace_file_mutex);
+		if (trace_file != -1 && !already_tracing) {
+
+				unsigned int alloc = 4; /* Allocation header */
+				__write_alloC(&alloc, sizeof(alloc));
+				__write_alloC(&param_1, sizeof(param_1));
+				__write_alloC(&param_2, sizeof(param_2));
+				__write_alloC(&mem, sizeof(mem));
+				flag_trace = 0;
+
+		}
+		(void) mutex_unlock(&trace_file_mutex);
+		/* end tracing */
+
       return mem;
     }
 #endif
@@ -4116,6 +4496,21 @@
     }
   }
 
+	/* Trace calloc() */
+	(void) mutex_lock(&trace_file_mutex);
+	if (trace_file != -1 && !already_tracing) {
+
+			unsigned int alloc = 4; /* Allocation header */
+			__write_alloC(&alloc, sizeof(alloc));
+			__write_alloC(&param_1, sizeof(param_1));
+			__write_alloC(&param_2, sizeof(param_2));
+			__write_alloC(&mem, sizeof(mem));
+			flag_trace = 0;
+
+	}
+	(void) mutex_unlock(&trace_file_mutex);
+	/* end tracing */
+
   return mem;
 }
 
