背景
循環壓力測試驅動某個模塊功能時,會出現alloc不到memory情況
工具
利用Linux自帶的工具(kmemleak),進行調試
怎樣編譯出工具
配置config,默認配置如下:
CONFIG_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=4000
CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y
修改後:
CONFIG_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=10000
CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=n
- 1.CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF配置爲n,表示默認編譯kmemleak模塊
void __init kmemleak_init(void)
{
int i;
unsigned long flags;
#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF //關掉此宏
if (!kmemleak_skip_disable) {
kmemleak_early_log = 0;
kmemleak_disable();//這個會關掉kmemleak功能
return;
}
#endif
- 2.如果CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE不修改的話,在初始化的時候會默認disable
static void __init log_early(int op_type, const void *ptr, size_t size,
int min_count)
{
unsigned long flags;
struct early_log *log;
if (kmemleak_error) {
/* kmemleak stopped recording, just count the requests */
crt_early_log++;
return;
}
//這裏會判斷log size是不是大於4000,如果不是,則disable
if (crt_early_log >= ARRAY_SIZE(early_log)) {
crt_early_log++;
kmemleak_disable();
return;
}
...
}
- 3.上面代碼在kernel/msm-4.9/mm/kmemleak.c文件中
配置好並且編譯kernel後,開始調試
- 查看目錄/sys/kernel/debug下有沒有kmemleak文件?有則配置成功,沒有請重新配置
- 抓取kernel log, cat /dev/kmsg | grep kmemleak
- 開啓掃描echo scan=on > /sys/kernel/debug/kmemleak
- 等待一會後看看log中是否會有kmemleak,再同步cat /sys/kernel/debug/kmemleak
- 我這邊有故意alloc memory後沒有釋放,cat log如下:
unreferenced object 0xffffffdf0aebd900 (size 4096):
comm "cat", pid 10908, jiffies 4295008846 (age 822.560s)
hex dump (first 32 bytes):
1c d9 eb 0a df ff ff ff 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 01 00 00 00 2f 73 64 63 ............/sdc
backtrace:
[<ffffff95e8637b64>] kmem_cache_alloc+0x1dc/0x2d8
[<ffffff95e865b844>] getname_kernel+0x3c/0x104
[<ffffff95e8e07900>] himax_vendor_read+0x64/0x300
[<ffffff95e86c46bc>] proc_reg_read+0x88/0xf4
[<ffffff95e8649cbc>] vfs_read+0x98/0x178
[<ffffff95e864b360>] SyS_read+0x60/0xc0
[<ffffff95e8483770>] el0_svc_naked+0x24/0x28
[<ffffffffffffffff>] 0xffffffffffffffff
- 每調用proc_reg_read後就會出現4K的內存泄漏,如果調用n次可想而知.
以此記錄