1*8d67ca89SAndroid Build Coastguard WorkerNative Memory Tracking using libc Callbacks 2*8d67ca89SAndroid Build Coastguard Worker------------------------------------------- 3*8d67ca89SAndroid Build Coastguard WorkerMalloc debug can be used to get information on all of the live allocations 4*8d67ca89SAndroid Build Coastguard Workerin a process. The libc library in Android exports two calls that can be 5*8d67ca89SAndroid Build Coastguard Workerused to gather this data from a process. This tracking can be enabled using 6*8d67ca89SAndroid Build Coastguard Workereither the backtrace option or the backtrace\_enabled\_on\_signal option. 7*8d67ca89SAndroid Build Coastguard Worker 8*8d67ca89SAndroid Build Coastguard WorkerThe function to gather the data: 9*8d67ca89SAndroid Build Coastguard Worker 10*8d67ca89SAndroid Build Coastguard Worker`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);` 11*8d67ca89SAndroid Build Coastguard Worker 12*8d67ca89SAndroid Build Coastguard Worker*info* is set to a buffer allocated by the call that contains all of 13*8d67ca89SAndroid Build Coastguard Workerthe allocation information. 14*8d67ca89SAndroid Build Coastguard Worker*overall\_size* is set to the total size of the buffer returned. If this 15*8d67ca89SAndroid Build Coastguard Worker*info\_size* 16*8d67ca89SAndroid Build Coastguard Workervalue is zero, then there are no allocation being tracked. 17*8d67ca89SAndroid Build Coastguard Worker*total\_memory* is set to the sum of all allocation sizes that are live at 18*8d67ca89SAndroid Build Coastguard Workerthe point of the function call. This does not include the memory allocated 19*8d67ca89SAndroid Build Coastguard Workerby the malloc debug library itself. 20*8d67ca89SAndroid Build Coastguard Worker*backtrace\_size* is set to the maximum number of backtrace entries 21*8d67ca89SAndroid Build Coastguard Workerthat are present for each allocation. 22*8d67ca89SAndroid Build Coastguard Worker 23*8d67ca89SAndroid Build Coastguard WorkerIn order to free the buffer allocated by the function, call: 24*8d67ca89SAndroid Build Coastguard Worker 25*8d67ca89SAndroid Build Coastguard Worker`extern "C" void free_malloc_leak_info(uint8_t* info);` 26*8d67ca89SAndroid Build Coastguard Worker 27*8d67ca89SAndroid Build Coastguard Worker### Format of info Buffer 28*8d67ca89SAndroid Build Coastguard Worker size_t size_of_original_allocation 29*8d67ca89SAndroid Build Coastguard Worker size_t num_allocations 30*8d67ca89SAndroid Build Coastguard Worker uintptr_t pc1 31*8d67ca89SAndroid Build Coastguard Worker uintptr_t pc2 32*8d67ca89SAndroid Build Coastguard Worker uintptr_t pc3 33*8d67ca89SAndroid Build Coastguard Worker . 34*8d67ca89SAndroid Build Coastguard Worker . 35*8d67ca89SAndroid Build Coastguard Worker . 36*8d67ca89SAndroid Build Coastguard Worker 37*8d67ca89SAndroid Build Coastguard WorkerThe number of *uintptr\_t* values is determined by the value 38*8d67ca89SAndroid Build Coastguard Worker*backtrace\_size* as returned by the original call to 39*8d67ca89SAndroid Build Coastguard Worker*get\_malloc\_leak\_info*. This value is not variable, it is the same 40*8d67ca89SAndroid Build Coastguard Workerfor all the returned data. The value 41*8d67ca89SAndroid Build Coastguard Worker*num\_allocations* contains the total number of allocations with the same 42*8d67ca89SAndroid Build Coastguard Workerbacktrace and size as this allocation. On Android Nougat, this value was 43*8d67ca89SAndroid Build Coastguard Workerincorrectly set to the number of frames in the backtrace. 44*8d67ca89SAndroid Build Coastguard WorkerEach *uintptr\_t* is a pc of the callstack. If the total number 45*8d67ca89SAndroid Build Coastguard Workerof backtrace entries is less than *backtrace\_size*, the rest of the 46*8d67ca89SAndroid Build Coastguard Workerentries are zero. 47*8d67ca89SAndroid Build Coastguard WorkerThe calls from within the malloc debug library are automatically removed. 48*8d67ca89SAndroid Build Coastguard Worker 49*8d67ca89SAndroid Build Coastguard WorkerFor 32 bit systems, *size\_t* and *uintptr\_t* are both 4 byte values. 50*8d67ca89SAndroid Build Coastguard Worker 51*8d67ca89SAndroid Build Coastguard WorkerFor 64 bit systems, *size\_t* and *uintptr\_t* are both 8 byte values. 52*8d67ca89SAndroid Build Coastguard Worker 53*8d67ca89SAndroid Build Coastguard WorkerThe total number of these structures returned in *info* is 54*8d67ca89SAndroid Build Coastguard Worker*overall\_size* divided by *info\_size*. 55*8d67ca89SAndroid Build Coastguard Worker 56*8d67ca89SAndroid Build Coastguard WorkerNote, the size value in each allocation data structure will have bit 31 set 57*8d67ca89SAndroid Build Coastguard Workerif this allocation was created in a process forked from the Zygote process. 58*8d67ca89SAndroid Build Coastguard WorkerThis helps to distinguish between native allocations created by the application. 59