2016-03-18 22:29:51 +01:00
|
|
|
Native Memory Tracking using libc Callbacks
|
|
|
|
-------------------------------------------
|
|
|
|
Malloc debug can be used to get information on all of the live allocations
|
|
|
|
in a process. The libc library in Android exports two calls that can be
|
|
|
|
used to gather this data from a process. This tracking can be enabled using
|
|
|
|
either the backtrace option or the backtrace\_enabled\_on\_signal option.
|
|
|
|
|
|
|
|
The function to gather the data:
|
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
`extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory, size_t* backtrace_size);`
|
2016-03-18 22:29:51 +01:00
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
*info* is set to a buffer allocated by the call that contains all of
|
2016-03-18 22:29:51 +01:00
|
|
|
the allocation information.
|
2016-04-27 01:07:29 +02:00
|
|
|
*overall\_size* is set to the total size of the buffer returned. If this
|
|
|
|
*info\_size*
|
2016-03-18 22:29:51 +01:00
|
|
|
value is zero, then there are no allocation being tracked.
|
2016-04-27 01:07:29 +02:00
|
|
|
*total\_memory* is set to the sum of all allocation sizes that are live at
|
2016-03-18 22:29:51 +01:00
|
|
|
the point of the function call. This does not include the memory allocated
|
|
|
|
by the malloc debug library itself.
|
2016-04-27 01:07:29 +02:00
|
|
|
*backtrace\_size* is set to the maximum number of backtrace entries
|
2016-03-18 22:29:51 +01:00
|
|
|
that are present for each allocation.
|
|
|
|
|
|
|
|
In order to free the buffer allocated by the function, call:
|
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
`extern "C" void free_malloc_leak_info(uint8_t* info);`
|
2016-03-18 22:29:51 +01:00
|
|
|
|
|
|
|
### Format of info Buffer
|
2016-04-27 01:07:29 +02:00
|
|
|
size_t size_of_original_allocation
|
2017-03-09 22:47:37 +01:00
|
|
|
size_t num_allocations
|
2016-04-27 01:07:29 +02:00
|
|
|
uintptr_t pc1
|
|
|
|
uintptr_t pc2
|
|
|
|
uintptr_t pc3
|
|
|
|
.
|
|
|
|
.
|
|
|
|
.
|
|
|
|
|
|
|
|
The number of *uintptr\_t* values is determined by the value
|
|
|
|
*backtrace\_size* as returned by the original call to
|
|
|
|
*get\_malloc\_leak\_info*. This value is not variable, it is the same
|
2016-03-18 22:29:51 +01:00
|
|
|
for all the returned data. The value
|
2017-03-09 22:47:37 +01:00
|
|
|
*num\_allocations* contains the total number of allocations with the same
|
|
|
|
backtrace and size as this allocation. On Android Nougat, this value was
|
|
|
|
incorrectly set to the number of frames in the backtrace.
|
|
|
|
Each *uintptr\_t* is a pc of the callstack. If the total number
|
|
|
|
of backtrace entries is less than *backtrace\_size*, the rest of the
|
|
|
|
entries are zero.
|
2016-03-18 22:29:51 +01:00
|
|
|
The calls from within the malloc debug library are automatically removed.
|
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
For 32 bit systems, *size\_t* and *uintptr\_t* are both 4 byte values.
|
2016-03-18 22:29:51 +01:00
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
For 64 bit systems, *size\_t* and *uintptr\_t* are both 8 byte values.
|
2016-03-18 22:29:51 +01:00
|
|
|
|
2016-04-27 01:07:29 +02:00
|
|
|
The total number of these structures returned in *info* is
|
|
|
|
*overall\_size* divided by *info\_size*.
|
2016-03-18 22:29:51 +01:00
|
|
|
|
|
|
|
Note, the size value in each allocation data structure will have bit 31 set
|
2016-09-28 23:51:12 +02:00
|
|
|
if this allocation was created in a process forked from the Zygote process.
|
|
|
|
This helps to distinguish between native allocations created by the application.
|