[memprof: 50/76] memintercept.c: Intercept calls to the WebKit allocator routines



commit 4a811b6b7ae768c1756301eb4484abb7c1a46111
Author: Holger Hans Peter Freyther <zecke selfish org>
Date:   Mon Oct 26 17:46:25 2009 +0100

    memintercept.c: Intercept calls to the WebKit allocator routines
    
    Allow to intercept calls to WebKit's WTF::fastMalloc and
    track malloc/free usage of it. This is still C code that
    is following the C++ name mangling and will only work on
    systems following the Common Vendor ABI. This limitation
    is good enough right now.
    
    The WebKit allocator (TCmalloc) is still used when fastMalloc
    is called which allows to look at memory fragmentation
    with the real allocator.
    
    * lib/memintercept.c:
    (_ZN3WTF10fastMallocEj): Add WTF::fastMalloc(unsigned int)
    (_ZN3WTF11fastReallocEPvj): Add WTF::fastRealloc(void *, unsigned int)
    (_ZN3WTF8fastFreeEPv): Add WTF::fastFree(void)
    (_ZN3WTF10fastCallocEjj): Add WTF::fastCalloc(unsigned int, unsigned int)
    (mi_init): Look for fastMalloc and such

 lib/memintercept.c |  102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 102 insertions(+), 0 deletions(-)
---
diff --git a/lib/memintercept.c b/lib/memintercept.c
index 3a0266b..19fec5e 100644
--- a/lib/memintercept.c
+++ b/lib/memintercept.c
@@ -3,6 +3,7 @@
 /* MemProf -- memory profiler and leak detector
  * Copyright 1999, 2000, 2001, Red Hat, Inc.
  * Copyright 2002, Kristian Rietveld
+ * Copyright 2009, Holger Hans Peter Freyther
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -38,6 +39,12 @@ static void *(*old_memalign) (size_t boundary, size_t size);
 static void *(*old_realloc) (void *ptr, size_t size);
 static void (*old_free) (void *ptr);
 
+/* helpers for WebKit */
+static void *(*old_fastMalloc) (size_t size);
+static void *(*old_fastCalloc) (size_t nmemb, size_t size);
+static void *(*old_fastRealloc) (void *ptr, size_t size);
+static void (*old_fastFree) (void *ptr);
+
 static void
 abort_unitialized (const char *call)
 {
@@ -215,6 +222,95 @@ free (void *ptr)
 	do_free (ptr);
 }
 
+void *
+_ZN3WTF10fastMallocEj(size_t size)
+{
+	void *result;
+	MIInfo info;
+
+	if (!mi_check_init ())
+		return NULL;/* See comment in initialize() */
+
+	result = (*old_fastMalloc) (size);
+
+	if (mi_tracing ()) {
+		info.alloc.operation = MI_MALLOC;
+		info.alloc.old_ptr = NULL;
+		info.alloc.new_ptr = result;
+		info.alloc.size = size;
+
+		mi_call_with_backtrace (2, mi_write_stack, &info);
+	}
+
+	return result;
+}
+
+void *
+_ZN3WTF11fastReallocEPvj(void *ptr, size_t size)
+{
+	void *result;
+	MIInfo info;
+
+	if (!mi_check_init ())
+		return NULL;/* See comment in initialize() */
+
+	result = (*old_fastRealloc) (ptr, size);
+
+	if (mi_tracing ()) {
+		info.alloc.operation = MI_REALLOC;
+		info.alloc.old_ptr = ptr;
+		info.alloc.new_ptr = result;
+		info.alloc.size = size;
+
+		mi_call_with_backtrace (1, mi_write_stack, &info);
+	}
+
+	return result;
+}
+
+void
+_ZN3WTF8fastFreeEPv(void *ptr)
+{
+	MIInfo info;
+
+	if (!mi_check_init ())
+		return;/* See comment in initialize() */
+
+	(*old_fastFree) (ptr);
+
+	if (mi_tracing ()) {
+		info.alloc.operation = MI_FREE;
+		info.alloc.old_ptr = ptr;
+		info.alloc.new_ptr = NULL;
+		info.alloc.size = 0;
+
+		mi_call_with_backtrace (1, mi_write_stack, &info);
+	}
+}
+
+void *
+_ZN3WTF10fastCallocEjj(size_t nmemb, size_t size)
+{
+	void *result;
+	MIInfo info;
+
+	if (!mi_check_init ())
+		return NULL;/* See comment in initialize() */
+
+	result = (*old_fastCalloc) (nmemb, size);
+
+	if (mi_tracing ()) {
+		info.alloc.operation = MI_MALLOC;
+		info.alloc.old_ptr = NULL;
+		info.alloc.new_ptr = result;
+		info.alloc.size = nmemb * size;
+
+		mi_call_with_backtrace (1, mi_write_stack, &info);
+	}
+
+	return result;
+}
+
 void
 mi_init (void)
 {
@@ -223,6 +319,12 @@ mi_init (void)
 	old_free = dlsym(RTLD_NEXT, "free");
 	old_calloc = dlsym(RTLD_NEXT, "calloc");
 	old_memalign = dlsym(RTLD_NEXT, "memalign");
+
+	/* to ease debugging webkit */
+	old_fastMalloc = dlsym(RTLD_NEXT, "_ZN3WTF10fastMallocEj");
+	old_fastRealloc = dlsym(RTLD_NEXT, "_ZN3WTF11fastReallocEPvj");
+	old_fastFree = dlsym(RTLD_NEXT, "_ZN3WTF8fastFreeEPv");
+	old_fastCalloc = dlsym(RTLD_NEXT, "_ZN3WTF10fastCallocEjj");
 }
 
 void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]