From 1034791258297bfcdb6098100ca74d1aba01a20a Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Thu, 14 Sep 2017 16:29:23 +0300 Subject: [PATCH] Add mmap/munmap to interpret's output. --- src/track/CMakeLists.txt | 3 ++ src/track/heaptrack_preload.cpp | 6 ++-- src/track/libheaptrack.cpp | 67 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/track/CMakeLists.txt b/src/track/CMakeLists.txt index f189bea..06f59b7 100644 --- a/src/track/CMakeLists.txt +++ b/src/track/CMakeLists.txt @@ -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 diff --git a/src/track/heaptrack_preload.cpp b/src/track/heaptrack_preload.cpp index cac0b84..22815ae 100644 --- a/src/track/heaptrack_preload.cpp +++ b/src/track/heaptrack_preload.cpp @@ -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(); diff --git a/src/track/libheaptrack.cpp b/src/track/libheaptrack.cpp index 7bd5d32..2ed4e1e 100644 --- a/src/track/libheaptrack.cpp +++ b/src/track/libheaptrack.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -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(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(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 s_locked; static LockedData* s_data; + + static size_t k_pageSize; }; atomic 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("heaptrack_mmap(%p, %zu, %d, %d, %d, %llu)", - ptr, length, prot, flags, fd, offset); + if (ptr && !RecursionGuard::isActive) { + RecursionGuard guard; - // TODO + debugLog("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("heaptrack_munmap(%p, %zu)", - ptr, length); + if (ptr && !RecursionGuard::isActive) { + RecursionGuard guard; + + debugLog("heaptrack_munmap(%p, %zu)", + ptr, length); - // TODO + HeapTrack heaptrack(guard); + heaptrack.handleMunmap(ptr, length); + } } void heaptrack_invalidate_module_cache() -- 2.7.4