メモリ漏れツールkmemleakの使用方法

2105 ワード

1.kmemleakの使い方
https://www.cnblogs.com/arnoldlu/p/8568090.html
2.英語のマニュアル
https://www.kernel.org/doc/html/latest/dev-tools/kmemleak.html
3.原理分析
http://blog.chinaunix.net/uid-26859697-id-5758036.html
上の3つの文章を参考しますが、カーネルをコンパイルしてからずっと  /sys/ケネル/debug/kmemleakの下には見つけられません。その後、ブートロゴの印刷が見つかりました。
[    0.000000] kmemleak: Kernel memory leak detector disabled
[    0.000550] kmemleak: Early log buffer exceeded (1147), please increase DEBUG_KMEMLEAK_EARLY_LOG_SIZE
后発见.config中DEBUG_KMEM LEAKEARLY_LOG_SIZE設定のデフォルト値は400です。10000に行ったら正常に動作します。
実例は以下の通りです
1.メモリ漏れの関連コードを書き出す:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct task_struct *mem_alloc_kthread;
int memory_kthread_alloc(void *arg)
{
	char *buf = NULL;
	while(!kthread_should_stop()) {
		buf = kmalloc(128, GFP_KERNEL);
		ssleep(5);
	}
	/* Should never get here */
	BUG_ON(1);
	return 0;
}

static int kmemleak_dbg_init(void)
{

	mem_alloc_kthread = kthread_create(memory_kthread_alloc, NULL, "mem_kthread");
	if (!IS_ERR(mem_alloc_kthread)) {
		wake_up_process(mem_alloc_kthread);
	}

	return 0;
}

static void kmemleak_dbg_exit(void)
{

	return 0;
}


module_init(kmemleak_dbg_init);

module_exit(kmemleak_dbg_exit);

MODULE_LICENSE("GPL");
2.ドライバを挿入したら、 echo scan>/sys/ケネル/debug/kmemleak 
3.メモリの漏洩状況を観察してください。
   cat/sys/ケネル/debug/kmemleak
unreferenced object 0xcbd86580 (size 128): //  128  
  comm "mem_kthread", pid 1676, jiffies 122870 (age 591.900s)
  hex dump (first 32 bytes):
    00 00 f2 cb 00 10 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 02 00 00 64 00 00 00 00  ...........d....
  backtrace://   ,      
    [] memory_kthread_alloc+0x18/0x3c [kmemleak]
    [] kthread+0xdc/0xf0
    [] ret_from_fork+0x14/0x2c
    [] 0xffffffff