diff options
author | Christopher Ferris <cferris@google.com> | 2019-03-01 17:59:51 -0800 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2019-03-07 08:39:55 -0800 |
commit | 6c619a0da3f96a26d91c1db48fd3e3be156aabe5 (patch) | |
tree | aa0802d01d19ed0891e6b77a732cc72ed5617dcc /libc/malloc_debug/malloc_debug.cpp | |
parent | 4e167f35d602f9dcede3b74ff3e09e88f3edb398 (diff) |
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)
Diffstat (limited to 'libc/malloc_debug/malloc_debug.cpp')
-rw-r--r-- | libc/malloc_debug/malloc_debug.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
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 <errno.h> #include <inttypes.h> #include <malloc.h> +#include <stdio.h> #include <string.h> #include <sys/cdefs.h> #include <sys/param.h> @@ -41,6 +42,7 @@ #include <android-base/properties.h> #include <android-base/stringprintf.h> #include <private/bionic_malloc_dispatch.h> +#include <private/MallocXmlElem.h> #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<ListInfoType> 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; |