From 6c619a0da3f96a26d91c1db48fd3e3be156aabe5 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Fri, 1 Mar 2019 17:59:51 -0800 Subject: Refactor the malloc_info code. malloc_info needs to be per native allocator, but the code treated it like a global function that doesn't depend on the native memory allocator. Update malloc debug to dump the actual pointers that it has been tracking. Test: bionic-unit-tests pass. Test: malloc debug tests pass. Test: malloc hook tests pass. Change-Id: I3b0d4d748489dd84c16d16933479dc8b8d79013e Merged-In: I3b0d4d748489dd84c16d16933479dc8b8d79013e (cherry picked from commit a3656a98b10d2a4a6194a5d9705ad9c2cc5877b0) --- libc/malloc_debug/malloc_debug.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'libc/malloc_debug/malloc_debug.cpp') diff --git a/libc/malloc_debug/malloc_debug.cpp b/libc/malloc_debug/malloc_debug.cpp index f66295706..093bdee8c 100644 --- a/libc/malloc_debug/malloc_debug.cpp +++ b/libc/malloc_debug/malloc_debug.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ #include #include #include +#include #include "Config.h" #include "DebugData.h" @@ -85,6 +87,7 @@ void* debug_realloc(void* pointer, size_t bytes); void* debug_calloc(size_t nmemb, size_t bytes); struct mallinfo debug_mallinfo(); int debug_mallopt(int param, int value); +int debug_malloc_info(int options, FILE* fp); int debug_posix_memalign(void** memptr, size_t alignment, size_t size); int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); @@ -725,6 +728,32 @@ int debug_mallopt(int param, int value) { return g_dispatch->mallopt(param, value); } +int debug_malloc_info(int options, FILE* fp) { + if (DebugCallsDisabled() || !g_debug->TrackPointers()) { + return g_dispatch->malloc_info(options, fp); + } + + MallocXmlElem root(fp, "malloc", "version=\"debug-malloc-1\""); + std::vector list; + PointerData::GetAllocList(&list); + + size_t alloc_num = 0; + for (size_t i = 0; i < list.size(); i++) { + MallocXmlElem alloc(fp, "allocation", "nr=\"%zu\"", alloc_num); + + size_t total = 1; + size_t size = list[i].size; + while (i < list.size() - 1 && list[i + 1].size == size) { + i++; + total++; + } + MallocXmlElem(fp, "size").Contents("%zu", list[i].size); + MallocXmlElem(fp, "total").Contents("%zu", total); + alloc_num++; + } + return 0; +} + void* debug_aligned_alloc(size_t alignment, size_t size) { if (DebugCallsDisabled()) { return g_dispatch->aligned_alloc(alignment, size); @@ -741,7 +770,7 @@ int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { return g_dispatch->posix_memalign(memptr, alignment, size); } - if (!powerof2(alignment)) { + if (alignment < sizeof(void*) || !powerof2(alignment)) { return EINVAL; } int saved_errno = errno; -- cgit v1.2.3