zoukankan      html  css  js  c++  java
  • hello.c内核模块编译 -- linux内核

    Linux开发模块,在本机上看调试信息的方法走通了。当前版本号2.6.32-32-generic

    uname –r

    能够查询

    这里取module_param()作为样例。

    该宏被定义在include/linux/moduleparam.h文件里,详细定义例如以下:

    #define module_param(name, type, perm)                
        module_param_named(name, name, type, perm)

    当中使用了 3个參数:要传递的參数变量名, 变量的数据类型, 以及訪问參数的权限。 

    hello.c

    #include <linux/init.h> 
    #include <linux/module.h>
    #include <linux/moduleparam.h>
    
    MODULE_LICENSE("Dual BSD/GPL");
    
    static char *flag="world"; 
    static int times = 5;
    module_param(times,int,S_IRUSR);
    module_param(flag,charp,S_IRUSR);
    
    static int hello_init(void)
    {
    	int i;
    	for(i=0;i<=times;i++)
    	{
    		printk("(%d)hello ,%s
    ",i,flag);  //KERN_DEBUG
    	}
    	return 0;
    }
    
    static void hello_exit(void)
    {
    	printk("Goodbye,%s
    ",flag); //KERN_DEBUG
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    

    这个文件须要编译成模块,採用

    Makefile

    obj-m	:= hello.o
    KERNELDIR := /lib/modules/$(shell uname -r)/build
    PWD       := $(shell pwd)
    
    default:
    	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    
    clean:
    	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions


    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    1),-C $(KERNELDIR)
    表示在$(KERNELDIR)文件夹下运行make命令。
    2),M=$(PWD)
    表示包括$(PWD)下的Makefile文件。
    3),modules
    表示模块编译

    在终端中

    make

    生成hello.ko模块

    Insmod hello.ko flag=”daniu” times=5

    root@zhangw:/mnt/hgfs/test_curl/core# dmesg -c
    [ 9057.070444] Goodbye,xffffffe2xffffff80xffffff9dxffffff80xffffff9ddaniuxffffffe2xffffff80xffffff9dxffffff80xffffff9d
    [ 9059.357777] (0)hello ,daniu
    [ 9059.357781] (1)hello ,daniu
    [ 9059.357783] (2)hello ,daniu
    [ 9059.357784] (3)hello ,daniu
    [ 9059.357785] (4)hello ,daniu
    [ 9059.357786] (5)hello ,daniu

    rmmod hello.ko


  • 相关阅读:
    【CF875E】Delivery Club 二分+线段树
    【CF316G3】Good Substrings 后缀自动机
    【BZOJ3413】匹配 离线+后缀树+树状数组
    【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线
    【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理
    【LOJ6254】最优卡组 堆(模拟搜索)
    面试问题总结
    Nginx基本配置
    Visual Studio Enterprise 2015下载 Update3
    .net 中生成二维码的组件
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4288766.html
Copyright © 2011-2022 走看看