美文网首页
在ubuntu机器上直接调试内核模块Makefile模板

在ubuntu机器上直接调试内核模块Makefile模板

作者: 小桥流水啦啦啦 | 来源:发表于2020-07-13 15:46 被阅读0次
    需求:在ubuntu机器上面直接调试内核模块,编译出ko文件,直接插入内核。
    1、Makefile实现
    vi Makefile
    
    obj-m := test.o
    CURRENT_PATH := $(shell pwd)
    LINUX_KERNEL := $(shell uname -r)
    LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)
    
    all:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
    clean:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
    
    2、test.c
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/gfp.h>
    
    MODULE_LICENSE("GPL");
    static int __init __get_free_pages_init(void);
    static void __exit __get_free_pages_exit(void);
    
    unsigned long addr = 0;
    
    int __init __get_free_pages_init(void)
    {
        addr = __get_free_pages( GFP_KERNEL, 3); //分配8个物理页
    
        if (!addr)
        {
            return -ENOMEM;
        }
        else
            printk("__get_free_pages Successfully!,\naddr = 0x%lx\n",addr);
    }
    
    void __exit __get_free_pages_exit(void)
    {
        if (addr)
        {
            free_pages(addr,3);
            printk("free_pages OK!\n");
        }
        printk("exit!\n");
    }
    
    module_init(__get_free_pages_init);
    module_exit(__get_free_pages_exit);
    
    3、调试
    3.1 插入模块
    insmod test.ko
    demsg -c
    [ 2103.082112] free_pages OK!
    [ 2103.082114] exit!
    [ 2540.899198] __get_free_pages Successfully!,
                   addr = 0xffff9a4599d90000
    [ 2540.899201] do_init_module: 'test'->init suspiciously returned 57, it should follow 0/-E convention
                   do_init_module: loading module anyway...
    [ 2540.899204] CPU: 2 PID: 11891 Comm: insmod Tainted: G           OE    4.15.0-30deepin-generic #31
    [ 2540.899205] Hardware name: IPASON /H310MHP, BIOS 5.12 07/18/2019
    [ 2540.899206] Call Trace:
    [ 2540.899211]  dump_stack+0x63/0x8b
    [ 2540.899213]  do_init_module+0x8d/0x219
    [ 2540.899215]  load_module+0x19bb/0x1dd0
    [ 2540.899217]  ? ima_post_read_file+0x83/0xa0
    [ 2540.899219]  SYSC_finit_module+0xe5/0x120
    [ 2540.899220]  ? SYSC_finit_module+0xe5/0x120
    [ 2540.899222]  SyS_finit_module+0xe/0x10
    [ 2540.899224]  do_syscall_64+0x73/0x130
    [ 2540.899226]  entry_SYSCALL_64_after_hwframe+0x3d/0xa2
    [ 2540.899231] RIP: 0033:0x7f52c7e0a229
    [ 2540.899232] RSP: 002b:00007ffc4fb34f98 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
    [ 2540.899233] RAX: ffffffffffffffda RBX: 0000563012d2a1f0 RCX: 00007f52c7e0a229
    [ 2540.899234] RDX: 0000000000000000 RSI: 0000563010b84638 RDI: 0000000000000003
    [ 2540.899235] RBP: 0000563010b84638 R08: 0000000000000000 R09: 00007f52c80c1ea0
    [ 2540.899236] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
    [ 2540.899237] R13: 0000563012d29130 R14: 0000000000000000 R15: 0000000000000000
    
    3.2 卸载模块
    [ 2686.817997] free_pages OK!
    [ 2686.817999] exit!
    

    相关文章

      网友评论

          本文标题:在ubuntu机器上直接调试内核模块Makefile模板

          本文链接:https://www.haomeiwen.com/subject/feoacktx.html