From 2e1a40a2033088e7f2e78424a8b163d3fcf84b1b Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Wed, 13 Jun 2018 10:46:34 -0700 Subject: Change heap dump format slightly. Bump the version from v1.1 to v1.2 and add a build fingerprint line. Update the heap dump documentation to match the new format and reflect what made it in P and what made it in Q. Update the unit tests for this change. Add -O0 to unit test code to make it easier to debug. Add an external function that can be used by the framework code so that there is only one way to dump the heap. Bug: 110095681 Test: Ran unit tests. Test: Did a dump of a real process and verified fingerprint. Test: Did a dump of a process without malloc debug enabled. Change-Id: I769a476cbeaf4c85c5d75bd6d6385f0e3add948c Merged-In: I769a476cbeaf4c85c5d75bd6d6385f0e3add948c (cherry picked from commit c84a2a2601a4112ca6e43a33defb989c1da8c2f4) --- libc/malloc_debug/malloc_debug.cpp | 58 +++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 23 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 836c33b86..1e7086c7d 100644 --- a/libc/malloc_debug/malloc_debug.cpp +++ b/libc/malloc_debug/malloc_debug.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -69,9 +70,10 @@ __BEGIN_DECLS bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, const char* options); void debug_finalize(); -bool debug_dump_heap(const char* file_name); +void debug_dump_heap(const char* file_name); void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory, size_t* backtrace_size); +bool debug_write_malloc_leak_info(FILE* fp); ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); void debug_free_malloc_leak_info(uint8_t* info); size_t debug_malloc_usable_size(void* pointer); @@ -813,28 +815,11 @@ void* debug_valloc(size_t size) { static std::mutex g_dump_lock; -bool debug_dump_heap(const char* file_name) { - ScopedDisableDebugCalls disable; - - std::lock_guard guard(g_dump_lock); - - FILE* fp = fopen(file_name, "w+e"); - if (fp == nullptr) { - error_log("Unable to create file: %s", file_name); - return false; - } - error_log("Dumping to file: %s\n", file_name); +static void write_dump(FILE* fp) { + fprintf(fp, "Android Native Heap Dump v1.2\n\n"); - if (!(g_debug->config().options() & BACKTRACE)) { - fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n"); - fprintf(fp, "# adb shell stop\n"); - fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n"); - fprintf(fp, "# adb shell start\n"); - fclose(fp); - return false; - } - - fprintf(fp, "Android Native Heap Dump v1.1\n\n"); + std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown"); + fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str()); PointerData::DumpLiveToFile(fp); @@ -846,6 +831,33 @@ bool debug_dump_heap(const char* file_name) { fprintf(fp, "%s", content.c_str()); } fprintf(fp, "END\n"); - fclose(fp); +} + +bool debug_write_malloc_leak_info(FILE* fp) { + ScopedDisableDebugCalls disable; + + std::lock_guard guard(g_dump_lock); + + if (!(g_debug->config().options() & BACKTRACE)) { + return false; + } + + write_dump(fp); return true; } + +void debug_dump_heap(const char* file_name) { + ScopedDisableDebugCalls disable; + + std::lock_guard guard(g_dump_lock); + + FILE* fp = fopen(file_name, "w+e"); + if (fp == nullptr) { + error_log("Unable to create file: %s", file_name); + return; + } + + error_log("Dumping to file: %s\n", file_name); + write_dump(fp); + fclose(fp); +} -- cgit v1.2.3