Export malloc_backtrace
Change-Id: Ic1adb4dfd86b9ca698443a36263a3df2c91edda3
diff --git a/libc/bionic/malloc_common.cpp b/libc/bionic/malloc_common.cpp
index b991b9a..863b07c 100644
--- a/libc/bionic/malloc_common.cpp
+++ b/libc/bionic/malloc_common.cpp
@@ -183,6 +183,7 @@
static void (*g_debug_finalize_func)();
static void (*g_debug_get_malloc_leak_info_func)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
static void (*g_debug_free_malloc_leak_info_func)(uint8_t*);
+static ssize_t (*g_debug_malloc_backtrace_func)(void*, uintptr_t*, size_t);
// =============================================================================
// Log functions
@@ -369,6 +370,15 @@
}
g_debug_free_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t*)>(sym);
+ sym = dlsym(malloc_impl_handle, "debug_malloc_backtrace");
+ if (sym == nullptr) {
+ error_log("%s: debug_malloc_backtrace routine not found in %s", getprogname(),
+ DEBUG_SHARED_LIB);
+ dlclose(malloc_impl_handle);
+ return;
+ }
+ g_debug_malloc_backtrace_func = reinterpret_cast<ssize_t (*)(void*, uintptr_t*, size_t)>(sym);
+
if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild)) {
dlclose(malloc_impl_handle);
return;
@@ -436,3 +446,16 @@
}
return Malloc(malloc_enable)();
}
+
+#ifndef LIBC_STATIC
+extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
+ if (g_debug_malloc_backtrace_func == nullptr) {
+ return 0;
+ }
+ return g_debug_malloc_backtrace_func(pointer, frames, frame_count);
+}
+#else
+extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
+ return 0;
+}
+#endif
diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map
index 1da85ea..2c0ab15 100644
--- a/libc/libc.arm.brillo.map
+++ b/libc/libc.arm.brillo.map
@@ -627,6 +627,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index bad3fec..c22535d 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -627,6 +627,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index c7aca4f..63c82e5 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -552,6 +552,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 1f4da93..c000fb1 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -630,6 +630,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map
index 3562ebf..8e5fdf4 100644
--- a/libc/libc.mips.brillo.map
+++ b/libc/libc.mips.brillo.map
@@ -625,6 +625,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 1f7b9aa..d30ead8 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -625,6 +625,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index c7aca4f..63c82e5 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -552,6 +552,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map
index c57a072..7ac0976 100644
--- a/libc/libc.x86.brillo.map
+++ b/libc/libc.x86.brillo.map
@@ -624,6 +624,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 56d6609..11fb710 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -624,6 +624,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index c7aca4f..63c82e5 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -552,6 +552,7 @@
madvise;
mallinfo;
malloc;
+ malloc_backtrace;
malloc_disable;
malloc_enable;
malloc_iterate;
diff --git a/libc/malloc_debug/exported32.map b/libc/malloc_debug/exported32.map
index 8f994f1..a985ef9 100644
--- a/libc/malloc_debug/exported32.map
+++ b/libc/malloc_debug/exported32.map
@@ -9,6 +9,7 @@
debug_iterate;
debug_mallinfo;
debug_malloc;
+ debug_malloc_backtrace;
debug_malloc_disable;
debug_malloc_enable;
debug_malloc_usable_size;
diff --git a/libc/malloc_debug/exported64.map b/libc/malloc_debug/exported64.map
index 8c47449..1a6b30f 100644
--- a/libc/malloc_debug/exported64.map
+++ b/libc/malloc_debug/exported64.map
@@ -9,6 +9,7 @@
debug_iterate;
debug_mallinfo;
debug_malloc;
+ debug_malloc_backtrace;
debug_malloc_disable;
debug_malloc_enable;
debug_malloc_usable_size;
diff --git a/libc/malloc_debug/malloc_debug.cpp b/libc/malloc_debug/malloc_debug.cpp
index 5b6e415..e079c7e 100644
--- a/libc/malloc_debug/malloc_debug.cpp
+++ b/libc/malloc_debug/malloc_debug.cpp
@@ -67,6 +67,7 @@
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);
+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);
void* debug_malloc(size_t size);
@@ -163,6 +164,7 @@
if (g_debug->config().options & BACKTRACE) {
BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
if (g_debug->backtrace->enabled()) {
+ ScopedDisableDebugCalls disable;
back_header->num_frames = backtrace_get(
&back_header->frames[0], g_debug->config().backtrace_frames);
backtrace_found = back_header->num_frames > 0;
@@ -616,6 +618,39 @@
g_dispatch->malloc_enable();
}
+ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
+ if (DebugCallsDisabled() || pointer == nullptr) {
+ return 0;
+ }
+
+ if (g_debug->need_header()) {
+ Header* header;
+ if (g_debug->config().options & TRACK_ALLOCS) {
+ header = g_debug->GetHeader(pointer);
+ if (!g_debug->track->Contains(header)) {
+ return 0;
+ }
+ } else {
+ header = reinterpret_cast<Header*>(pointer);
+ }
+ if (header->tag != DEBUG_TAG) {
+ return 0;
+ }
+ if (g_debug->config().options & BACKTRACE) {
+ BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
+ if (back_header->num_frames > 0) {
+ if (frame_count > back_header->num_frames) {
+ frame_count = back_header->num_frames;
+ }
+ memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
+ return frame_count;
+ }
+ }
+ }
+
+ return 0;
+}
+
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
void* debug_pvalloc(size_t bytes) {
if (DebugCallsDisabled()) {