android malloc_debug tool

android malloc_debug tool

130|console:/ # cat /data/local.prop                                           
libc.debug.malloc.options=backtrace=16 guard=8 fill_on_free=16 free_track=2046 leak_track
libc.debug.malloc.program=slub_debug_test.bin

在/data/local.prop里写了上述内容后,一般需要重启下系统,重启后看下slub_debug_test.bin进程的maps里是否有链接libc_malloc_debug.so,如果有,说明正常。

上面options里加了leak_track,但是只有在slub_debug_test.bin退出后,才会将mem leak信息打印出来,这有些鸡肋..

打印出来的log在logcat,例如:

console:/ # logcat |grep malloc_debug
09-22 15:24:56.400  4914  4914 E malloc_debug: +++ slub_debug_test.bin leaked block of size 16384 at 0x7b690a17f0 (leak 1 of 2)
09-22 15:24:56.400  4914  4914 E malloc_debug: Backtrace at time of allocation:
09-22 15:24:56.402  4914  4914 E malloc_debug:           #00  pc 00000000000011f0  /vendor/bin/slub_debug_test.bin
09-22 15:24:56.402  4914  4914 E malloc_debug:           #01  pc 000000000007d844  /apex/com.android.runtime/lib64/bionic/libc.so (__libc_init+108)
09-22 15:24:56.402  4914  4914 E malloc_debug:           #02  pc 0000000000000000  <unknown>
09-22 15:24:56.402  4914  4914 E malloc_debug: +++ slub_debug_test.bin leaked block of size 8 at 0x7b6900a5b0 (leak 2 of 2)
09-22 15:24:56.402  4914  4914 E malloc_debug: Backtrace at time of allocation:
09-22 15:24:56.404  4914  4914 E malloc_debug:           #00  pc 00000000000d64e8  /apex/com.android.runtime/lib64/bionic/libc.so
09-22 15:24:56.404  4914  4914 E malloc_debug:           #01  pc 000000000008b208  /apex/com.android.runtime/lib64/bionic/libc.so (newlocale+160)
09-22 15:24:56.404  4914  4914 E malloc_debug:           #02  pc 000000000009956c  /system/lib64/vndk-sp-29/libc++.so
09-22 15:24:56.404  4914  4914 E malloc_debug:           #03  pc 000000000009f1b8  /system/lib64/vndk-sp-29/libc++.so (std::__1::locale::__global()+160)
09-22 15:24:56.404  4914  4914 E malloc_debug:           #04  pc 000000000009f218  /system/lib64/vndk-sp-29/libc++.so (std::__1::locale::locale()+16)
09-22 15:24:56.404  4914  4914 E malloc_debug:           #05  pc 0000000000075c5c  /system/lib64/vndk-sp-29/libc++.so (std::__1::basic_streambuf<char, std::__1::char_traits<char>>::basic_streambuf()+36)
09-22 15:24:56.404  4914  4914 E malloc_debug:           #06  pc 00000000000815d4  /system/lib64/vndk-sp-29/libc++.so (std::__1::ios_base::Init::Init()+68)
09-22 15:24:56.404  4914  4914 E malloc_debug:           #07  pc 0000000000082890  /system/lib64/vndk-sp-29/libc++.so
09-22 15:24:56.404  4914  4914 E malloc_debug:           #08  pc 0000000000050d08  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #09  pc 0000000000050f20  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #10  pc 0000000000050e2c  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #11  pc 000000000004ce70  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #12  pc 000000000004c03c  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #13  pc 00000000000533f4  /apex/com.android.runtime/bin/linker64
09-22 15:24:56.404  4914  4914 E malloc_debug:           #14  pc 0000000000000000  <unknown>

slub_debug_test.bin main函数如下,malloc一个16KB后的buffer后就return了:

...
unsigned char *ptr = (unsigned char *)malloc(16*1024);
printf("the alloced mem: 0x%lx.
", (unsigned long)ptr);

return 0;
}

reference:

https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md

原文地址:https://www.cnblogs.com/aspirs/p/15331417.html