Add mmap/munmap to interpret's output.
authorRuben Ayrapetyan <r.ayrapetyan@samsung.com>
Thu, 14 Sep 2017 13:29:23 +0000 (16:29 +0300)
committerRuben Ayrapetyan <r.ayrapetyan@samsung.com>
Thu, 14 Sep 2017 13:08:53 +0000 (16:08 +0300)
src/track/CMakeLists.txt
src/track/heaptrack_preload.cpp
src/track/libheaptrack.cpp

index f189bea..06f59b7 100644 (file)
@@ -18,11 +18,14 @@ add_library(heaptrack_preload MODULE
     libheaptrack.cpp
 )
 
+target_compile_options(heaptrack_preload PRIVATE "-ftls-model=initial-exec")
+
 target_link_libraries(heaptrack_preload LINK_PRIVATE
     ${CMAKE_DL_LIBS}
     ${CMAKE_THREAD_LIBS_INIT}
     ${LIBUNWIND_LIBRARY}
     rt
+    -ftls-model=initial-exec
 )
 
 set_target_properties(heaptrack_preload PROPERTIES
index cac0b84..22815ae 100644 (file)
@@ -271,7 +271,7 @@ void *mmap(void *addr,
            int prot,
            int flags,
            int fd,
-           off_t offset)
+           off_t offset) noexcept
 {
     if (!hooks::mmap) {
         hooks::init();
@@ -291,7 +291,7 @@ void *mmap64(void *addr,
              int prot,
              int flags,
              int fd,
-             off64_t offset)
+             off64_t offset) noexcept
 {
     if (!hooks::mmap64) {
         hooks::init();
@@ -307,7 +307,7 @@ void *mmap64(void *addr,
 }
 
 int munmap(void *addr,
-           size_t length)
+           size_t length) noexcept
 {
     if (!hooks::munmap) {
         hooks::init();
index 7bd5d32..2ed4e1e 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdio_ext.h>
 #include <pthread.h>
 #include <signal.h>
+#include <unistd.h>
 
 #include <atomic>
 #include <cinttypes>
@@ -273,6 +274,8 @@ public:
         writeCommandLine(out);
         writeSystemInfo(out);
 
+        k_pageSize = sysconf(_SC_PAGESIZE);
+
         s_data = new LockedData(out, stopCallback);
 
         if (initAfterCallback) {
@@ -392,6 +395,42 @@ public:
         }
     }
 
+    void handleMmap(void* ptr,
+                    size_t length,
+                    int prot,
+                    int fd,
+                    const Trace& trace)
+    {
+        if (!s_data || !s_data->out) {
+            return;
+        }
+        updateModuleCache();
+        const auto index = s_data->traceTree.index(trace, s_data->out);
+
+        size_t alignedLength = ((length + k_pageSize - 1) / k_pageSize) * k_pageSize;
+
+        if (fprintf(s_data->out, "* %zx %x %d %x %" PRIxPTR "\n",
+                    alignedLength, prot, fd, index, reinterpret_cast<uintptr_t>(ptr)) < 0) {
+            writeError();
+            return;
+        }
+    }
+
+    void handleMunmap(void* ptr,
+                      size_t length)
+    {
+        if (!s_data || !s_data->out) {
+            return;
+        }
+
+        size_t alignedLength = ((length + k_pageSize - 1) / k_pageSize) * k_pageSize;
+
+        if (fprintf(s_data->out, "/ %zx %" PRIxPTR "\n", alignedLength, reinterpret_cast<uintptr_t>(ptr)) < 0) {
+            writeError();
+            return;
+        }
+    }
+
 private:
     static int dl_iterate_phdr_callback(struct dl_phdr_info* info, size_t /*size*/, void* data)
     {
@@ -636,10 +675,13 @@ private:
 
     static atomic<bool> s_locked;
     static LockedData* s_data;
+
+    static size_t k_pageSize;
 };
 
 atomic<bool> HeapTrack::s_locked{false};
 HeapTrack::LockedData* HeapTrack::s_data{nullptr};
+size_t HeapTrack::k_pageSize{0u};
 }
 extern "C" {
 
@@ -716,18 +758,31 @@ void heaptrack_realloc(void* ptr_in, size_t size, void* ptr_out)
 
 void heaptrack_mmap(void* ptr, size_t length, int prot, int flags, int fd, off64_t offset)
 {
-    debugLog<VeryVerboseOutput>("heaptrack_mmap(%p, %zu, %d, %d, %d, %llu)",
-                                ptr, length, prot, flags, fd, offset);
+    if (ptr && !RecursionGuard::isActive) {
+        RecursionGuard guard;
 
-    // TODO
+        debugLog<VeryVerboseOutput>("heaptrack_mmap(%p, %zu, %d, %d, %d, %llu)",
+                                    ptr, length, prot, flags, fd, offset);
+
+        Trace trace;
+        trace.fill(2 + HEAPTRACK_DEBUG_BUILD);
+
+        HeapTrack heaptrack(guard);
+        heaptrack.handleMmap(ptr, length, prot, fd, trace);
+    }
 }
 
 void heaptrack_munmap(void* ptr, size_t length)
 {
-    debugLog<VeryVerboseOutput>("heaptrack_munmap(%p, %zu)",
-                                ptr, length);
+    if (ptr && !RecursionGuard::isActive) {
+        RecursionGuard guard;
+
+        debugLog<VeryVerboseOutput>("heaptrack_munmap(%p, %zu)",
+                                    ptr, length);
 
-    // TODO
+        HeapTrack heaptrack(guard);
+        heaptrack.handleMunmap(ptr, length);
+    }
 }
 
 void heaptrack_invalidate_module_cache()