From 7bd01783a830f72c1245c262a7fd9a199e90aed1 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Wed, 20 Apr 2016 12:30:58 -0700 Subject: Add the record alloc option. This option adds the ability to record all of the allocation requests and dump them to a file when a signal is sent to the process. Included in this change, redo the option processing to add a new string option. Bug: 27747898 Change-Id: Ida043362e38b5eb1d459c99db9c2581015dab366 --- libc/malloc_debug/malloc_debug.cpp | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (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 5da5b88e4..d2bcf9967 100644 --- a/libc/malloc_debug/malloc_debug.cpp +++ b/libc/malloc_debug/malloc_debug.cpp @@ -328,7 +328,13 @@ void* debug_malloc(size_t size) { } ScopedDisableDebugCalls disable; - return internal_malloc(size); + void* pointer = internal_malloc(size); + + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new MallocEntry(pointer, size)); + } + + return pointer; } static void internal_free(void* pointer) { @@ -393,6 +399,10 @@ void debug_free(void* pointer) { } ScopedDisableDebugCalls disable; + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new FreeEntry(pointer)); + } + internal_free(pointer); } @@ -461,6 +471,10 @@ void* debug_memalign(size_t alignment, size_t bytes) { memset(pointer, g_debug->config().fill_alloc_value, bytes); } + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment)); + } + return pointer; } @@ -471,10 +485,18 @@ void* debug_realloc(void* pointer, size_t bytes) { ScopedDisableDebugCalls disable; if (pointer == nullptr) { - return internal_malloc(bytes); + pointer = internal_malloc(bytes); + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr)); + } + return pointer; } if (bytes == 0) { + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer)); + } + internal_free(pointer); return nullptr; } @@ -555,6 +577,10 @@ void* debug_realloc(void* pointer, size_t bytes) { } } + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer)); + } + return new_pointer; } @@ -582,6 +608,7 @@ void* debug_calloc(size_t nmemb, size_t bytes) { return nullptr; } + void* pointer; if (g_debug->need_header()) { // The above check will guarantee the multiply will not overflow. if (size > Header::max_size()) { @@ -596,10 +623,14 @@ void* debug_calloc(size_t nmemb, size_t bytes) { return nullptr; } memset(header, 0, g_dispatch->malloc_usable_size(header)); - return InitHeader(header, header, size); + pointer = InitHeader(header, header, size); } else { - return g_dispatch->calloc(1, real_size); + pointer = g_dispatch->calloc(1, real_size); } + if (g_debug->config().options & RECORD_ALLOCS) { + g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb)); + } + return pointer; } struct mallinfo debug_mallinfo() { -- cgit v1.2.3