Linux-3.14.12内存管理笔记【内存泄漏检测kmemleak示例】【转】
本文转载自:http://blog.chinaunix.net/uid-26859697-id-5758037.html
分析完kmemleak实现后,照常实验一下,以确定功能正常。
如kmemcheck一样,该功能需要在内核开启的情况下才能够使用。主要的配置项有:CONFIG_DEBUG_KERNEL、CONFIG_HAVE_DEBUG_KMEMLEAK、CONFIG_DEBUG_KMEMLEAK,以及配置信息记录条数的CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE,通常情况下配置数量的可以不必修改,但是针对自启动初始化的模块,还是有必要进行调整的,以便记录更多的信息,避免遗漏。
以上几项的在make menuconfig编译配置内核时,配置路径如下:
Kernel hacking
+——>Kernel Debugging(该项需要开启)
+——>Memory Debugging
+——>Kernel memory leak detector(开启该项)
当然,这几项配置项是对其他配置项存在一定的依赖的,具体可以在make menuconfig反斜杠搜索对应配置项进行了解。
设置的过程中有几项需要特别注意的就是,在“Kernel memory leak detector”开启后展开的几项子项注意不要进行设置了,其中有:“Simple test for the kernel memory leak detector”和“Default kmemleak to off”。
例如实验中配置选项设置如下:
编译好内核之后,安装该调测版本的内核,重新启动linux系统,进入新内核。将会能够查看到/sys/kernel/debug/kmemleak该路径文件的存在。如果存在,则表明开启成功。
该实验仍使用64位系统做演示,修改前面kmemcheck的实验代码,构造内存泄漏。具体代码如下:
- #include <linux/init.h>
 - #include <linux/module.h>
 - #include <linux/mm.h>
 - #include <linux/slab.h>
 - #include <asm/page.h>
 - void kmemleak_memalloc(void)
 - {
 - char *pmem;
 - pmem = kmalloc(300, GFP_KERNEL);
 - if (!pmem)
 - {
 - printk("[Kmemleak]: kmalloc fail!\n");
 - }
 - else
 - {
 - printk("[Kmemleak]: kmalloc return %p \n", pmem);
 - }
 - }
 - int __init kmemleak_test_init(void)
 - {
 - printk("[Kmemleak]: kmemleak_test_init! \n");
 - kmemleak_memalloc();
 - return 0;
 - }
 - void __exit kmemleak_test_exit(void)
 - {
 - printk("[Kmemleak]: kmemleak_test_exit now \n");
 - }
 - module_init(kmemleak_test_init)
 - module_exit(kmemleak_test_exit)
 - MODULE_LICENSE("GPL");
 
Makefile脚本稍作修改后:
- obj-m = kmemleak_test.o
 - all:
 - make -C /lib/modules/`uname -r`/build M=`pwd`
 - clean:
 - rm -f *.o *.ko *.mod.c modules.order Module.symvers
 
编译内核ko后,通过ismod命令安装模块,根据代码的编写打印的日志信息,通过dmesg进行确认模块初始化完毕且内存申请妥当。
    相关信息如下:![]()
为了保证实验的成功,ko安装完毕后,等待了几分钟后。
通过命令:echo scan > /sys/kernel/debug/kmemleak,进行内存扫描检测。检测结果将会在/sys/kernel/debug/kmemleak文件中记录。通过dmesg的信息可以看到申请的内存空间位于0xffff95c9bf81b600地址。搜索一下,便可以看到kmemleak中的信息记录:
kmemleak的检测结果信息中,清晰地记录了申请该内存的调用栈信息、内存头32byte的信息以及泄漏的内存地址及大小。但是这里的大小并不是一个很精确的大小,这里呈现的是一个slab内存片的大小,实验中申请的空间大小为300字节,对应为512字节的slab片。具体的结合代码进行分析部分就省略掉了,那是开发人员必备的基础能力,鄙人就不班门弄斧了。
接下来细述一下kmemleak的控制参数,控制kmemleak主要是通过往/sys/kernel/debug/kmemleak文件中写入参数。例如前面使能kmemleak进行内存泄漏扫描的命令是写入scan,具体的命令则是:echo scan > /sys/kernel/debug/kmemleak。而清除kmemleak的扫描结果使用的参数是clear,对应的命令是:echo clear > /sys/kernel/debug/kmemleak。
更多的kmemleak参数有:
- off - disable kmemleak (irreversible)
 - stack=on - enable the task stacks scanning (default)
 - stack=off - disable the tasks stacks scanning
 - scan=on - start the automatic memory scanning thread (default)
 - scan=off - stop the automatic memory scanning thread
 - scan=<secs> - set the automatic memory scanning period in seconds
 - (default 600, 0 to stop the automatic scanning)
 - scan - trigger a memory scan
 - clear - clear list of current memory leak suspects, done by
 - marking all current reported unreferenced objects grey
 - dump=<addr> - dump information about the object found at <addr>
 
如果要是测试内核模块,正确的操作步骤应为:
1、 清除kmemleak的历史信息,以便记录新数据:
#echo clear > /sys/kernel/debug/kmemleak
2、 加载需要测试的内核模块,并执行相关测试用例;
#ismod ***.ko
3、 使能kmemleak进行内存泄漏扫描:
#echo scan > /sys/kernel/debug/kmemleak
4、 查看分析扫描结果:
#cat /sys/kernel/debug/kmemleak
至此,实验完毕,kmemleak挺好用的,适用于内核模块开发用于检测定位内存泄漏问题。
                    
                
                
            
        
浙公网安备 33010602011771号