From 607ac916ded850874386b8983cc9b09b7d525367 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Fri, 10 Apr 2015 16:42:29 +0200 Subject: [PATCH] malloc_debug: Impl. heap_debug_dump_allocations in guarded heap. --- .../posix/malloc_debug/guarded_heap.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp index ebb73bdbe0..b1c0a3e197 100644 --- a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp +++ b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp @@ -902,6 +902,48 @@ dump_guarded_heap(guarded_heap& heap) } +static void +dump_allocations(guarded_heap& heap, bool statsOnly, thread_id thread) +{ + WriteLocker heapLocker(heap.lock); + + size_t allocationCount = 0; + size_t allocationSize = 0; + for (guarded_heap_area* area = heap.areas; area != NULL; + area = area->next) { + + MutexLocker areaLocker(area->lock); + for (size_t i = 0; i < area->page_count; i++) { + guarded_heap_page& page = area->pages[i]; + if ((page.flags & GUARDED_HEAP_PAGE_FLAG_FIRST) == 0 + || (page.flags & GUARDED_HEAP_PAGE_FLAG_DEAD) != 0) { + continue; + } + + if (thread >= 0 && thread != page.thread) + continue; + + allocationCount++; + allocationSize += page.allocation_size; + + if (statsOnly) + continue; + + printf("allocation: base: %p; size: %" B_PRIuSIZE + "; thread: %" B_PRId32 "; alignment: %" B_PRIuSIZE "\n", + page.allocation_base, page.allocation_size, page.thread, + page.alignment); + + guarded_heap_print_stack_trace(page.stack_trace, + page.alloc_stack_trace_depth); + } + } + + printf("total allocations: %" B_PRIuSIZE ", %" B_PRIuSIZE " bytes\n", + allocationCount, allocationSize); +} + + // #pragma mark - Heap Debug API @@ -961,6 +1003,7 @@ heap_debug_validate_walls() extern "C" void heap_debug_dump_allocations(bool statsOnly, thread_id thread) { + dump_allocations(sGuardedHeap, statsOnly, thread); }