Add simple malloc/free tracer
authorMilian Wolff <mail@milianw.de>
Thu, 15 May 2014 12:47:25 +0000 (14:47 +0200)
committerMilian Wolff <mail@milianw.de>
Thu, 15 May 2014 12:47:25 +0000 (14:47 +0200)
CMakeLists.txt
malloctrace.cpp [new file with mode: 0644]
run.sh
trace.sh [new file with mode: 0755]

index 150254e..5a1080c 100644 (file)
@@ -9,6 +9,9 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")
 add_executable(mallocinfo main.cpp)
 target_link_libraries(mallocinfo dumpmallocinfo)
 
+add_library(malloctrace SHARED malloctrace.cpp)
+target_link_libraries(malloctrace -ldl -lunwind -lunwind-x86_64)
+
 add_executable(test_c test.c)
 
 install(TARGETS mallocinfo RUNTIME DESTINATION bin)
diff --git a/malloctrace.cpp b/malloctrace.cpp
new file mode 100644 (file)
index 0000000..cadb00a
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2014 Milian Wolff <mail@milianw.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <cstdio>
+#include <cassert>
+#include <cstring>
+#include <cstdlib>
+
+#include <dlfcn.h>
+#include <libunwind.h>
+
+namespace {
+
+using malloc_t = void* (*) (size_t);
+using free_t = void (*) (void*);
+
+malloc_t real_malloc = nullptr;
+free_t real_free = nullptr;
+
+struct InitializeMallocTrace
+{
+    InitializeMallocTrace()
+    {
+        real_malloc = reinterpret_cast<malloc_t>(dlsym(RTLD_NEXT, "malloc"));
+        real_free = reinterpret_cast<free_t>(dlsym(RTLD_NEXT, "free"));
+    }
+
+    ~InitializeMallocTrace()
+    {
+        real_malloc = 0;
+        real_free = nullptr;
+    }
+};
+
+static InitializeMallocTrace initializeMallocTrace;
+
+void show_backtrace()
+{
+    const size_t BUFSIZE = 256;
+    char name[BUFSIZE];
+    unw_cursor_t cursor; unw_context_t uc;
+    unw_word_t ip, sp, offp;
+
+    unw_getcontext (&uc);
+    unw_init_local (&cursor, &uc);
+
+    while (unw_step(&cursor) > 0)
+    {
+        name[0] = '\0';
+        unw_get_proc_name(&cursor, name, BUFSIZE, &offp);
+        unw_get_reg(&cursor, UNW_REG_IP, &ip);
+        unw_get_reg(&cursor, UNW_REG_SP, &sp);
+
+        if (name[0] == '\0') {
+            strcpy(name, "??");
+        }
+
+        printf("%s ip = %lx, sp = %lx\n", name, (long) ip, (long) sp);
+        if (!strcmp(name, "main")) {
+            break;
+        }
+    }
+}
+
+}
+
+extern "C" {
+
+void* malloc(size_t size)
+{
+    assert(real_malloc);
+    void* ret = real_malloc(size);
+    show_backtrace();
+    return ret;
+}
+
+void free(void* ptr)
+{
+    assert(real_free);
+    real_free(ptr);
+}
+
+}
diff --git a/run.sh b/run.sh
index 71c15d3..b19f4e6 100755 (executable)
--- a/run.sh
+++ b/run.sh
@@ -1,5 +1,10 @@
 #!/bin/bash
 
+if [[ -z "$2" ]]; then
+    echo "$0 INTERVAL DEBUGEE ARGS"
+    exit 1
+fi
+
 interval=$1
 shift 1
 
diff --git a/trace.sh b/trace.sh
new file mode 100755 (executable)
index 0000000..c9620bc
--- /dev/null
+++ b/trace.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+if [[ -z "$1" ]]; then
+    echo "$0 DEBUGEE ARGS"
+    exit 1
+fi
+
+output=$(pwd)/malloctrace.$1
+
+cb
+LD_PRELOAD=./libmalloctrace.so DUMP_MALLOC_TRACE_OUTPUT="$output" $@ &
+pid=$!
+wait $pid
+# bzip2 "$output.$pid"