From c0352bbd5f8bd4c7696d3b71a0056dcfb2b44708 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 25 May 2017 18:26:37 -0700 Subject: [PATCH] Add offset to backtrace_string printing. The libmemunreachable code uses backtrace_string to print out the backtrace information. However, when the offset of the map that a frame is in is non-zero, no information is printed. This means that when a frame comes from a shared library loaded from an apk, it's impossible to tell which shared library the frame is really coming from. Add the offset display when it's non-zero. This means this can now be fed to development/scripts/stack to decode the frame. Bug: 37276041 Test: Ran dumpsys meminfo --unreachable on an app that leaks from a Test: shared library and verified the offset output is present. Change-Id: I61d34ae3f617622d354cc099eff520a64782b6e2 --- libc/malloc_debug/backtrace.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libc/malloc_debug/backtrace.cpp b/libc/malloc_debug/backtrace.cpp index 88f5a1de4..907944f12 100644 --- a/libc/malloc_debug/backtrace.cpp +++ b/libc/malloc_debug/backtrace.cpp @@ -153,18 +153,27 @@ std::string backtrace_string(const uintptr_t* frames, size_t frame_count) { if (soname == nullptr) { soname = ""; } + + char offset_buf[128]; + if (entry != nullptr && entry->offset != 0) { + snprintf(offset_buf, sizeof(offset_buf), " (offset 0x%" PRIxPTR ")", entry->offset); + } else { + offset_buf[0] = '\0'; + } + char buf[1024]; if (symbol != nullptr) { char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr); const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol; async_safe_format_buffer( - buf, sizeof(buf), " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")\n", frame_num, - rel_pc, soname, best_name, frames[frame_num] - offset); + buf, sizeof(buf), " #%02zd pc %" PAD_PTR " %s%s (%s+%" PRIuPTR ")\n", frame_num, + rel_pc, soname, offset_buf, best_name, frames[frame_num] - offset); free(demangled_symbol); } else { async_safe_format_buffer( - buf, sizeof(buf), " #%02zd pc %" PAD_PTR " %s\n", frame_num, rel_pc, soname); + buf, sizeof(buf), " #%02zd pc %" PAD_PTR " %s%s\n", frame_num, rel_pc, soname, + offset_buf); } str += buf; } -- 2.11.0