Move all leak info functions to android_mallopt.

Bug: 130028357

Test: malloc_hooks unit tests.
Test: Enable backtrace for mediaserver, run dumpsys media.player -m
Test: Enable backtrace for calendar, run am dumpheap -n <PID> <FILE>
Change-Id: I6774e28ccd9b3f2310127a5b39ccd15fe696a787
Merged-In: I6774e28ccd9b3f2310127a5b39ccd15fe696a787
(cherry picked from commit 3aadc5e80a5e2cf6b6760ed90d528709223bb449)
diff --git a/libc/private/bionic_malloc.h b/libc/private/bionic_malloc.h
index e8a6f6e..9c602ea 100644
--- a/libc/private/bionic_malloc.h
+++ b/libc/private/bionic_malloc.h
@@ -30,6 +30,22 @@
 
 #include <stdbool.h>
 
+// Structures for android_mallopt.
+
+typedef struct {
+  // Pointer to the buffer allocated by a call to M_GET_MALLOC_LEAK_INFO.
+  uint8_t* buffer;
+  // The size of the "info" buffer.
+  size_t overall_size;
+  // The size of a single entry.
+  size_t info_size;
+  // The sum of all allocations that have been tracked. Does not include
+  // any heap overhead.
+  size_t total_memory;
+  // The maximum number of backtrace entries.
+  size_t backtrace_size;
+} android_mallopt_leak_info_t;
+
 // Opcodes for android_mallopt.
 
 enum {
@@ -48,6 +64,26 @@
   // Called after the zygote forks to indicate this is a child.
   M_SET_ZYGOTE_CHILD = 4,
 #define M_SET_ZYGOTE_CHILD M_SET_ZYGOTE_CHILD
+
+  // Options to dump backtraces of allocations. These options only
+  // work when malloc debug has been enabled.
+
+  // Writes the backtrace information of all current allocations to a file.
+  // NOTE: arg_size has to be sizeof(FILE*) because FILE is an opaque type.
+  //   arg = FILE*
+  //   arg_size = sizeof(FILE*)
+  M_WRITE_MALLOC_LEAK_INFO_TO_FILE = 5,
+#define M_WRITE_MALLOC_LEAK_INFO_TO_FILE M_WRITE_MALLOC_LEAK_INFO_TO_FILE
+  // Get information about the backtraces of all
+  //   arg = android_mallopt_leak_info_t*
+  //   arg_size = sizeof(android_mallopt_leak_info_t)
+  M_GET_MALLOC_LEAK_INFO = 6,
+#define M_GET_MALLOC_LEAK_INFO M_GET_MALLOC_LEAK_INFO
+  // Free the memory allocated and returned by M_GET_MALLOC_LEAK_INFO.
+  //   arg = android_mallopt_leak_info_t*
+  //   arg_size = sizeof(android_mallopt_leak_info_t)
+  M_FREE_MALLOC_LEAK_INFO = 7,
+#define M_FREE_MALLOC_LEAK_INFO M_FREE_MALLOC_LEAK_INFO
 };
 
 // Manipulates bionic-specific handling of memory allocation APIs such as