/*模块加载是向内核传递参数 如果不传递就用默认的参数*/
/*用户向内核传递参数的方法 eg insmod param.ko name="wenhui" age=20*/
/*内核调试信息的使用*/
#undef PDEBUG /*取消对PDEBUG的定义 以防重复定义*/
#ifdef XXX_DEBUG
#define PDEBUG(fmt) printk("<0>XXX:%d\n",fmt)
#else
#define PDEBUG(fmt) /*调试被关闭不做任何事情*/
#endif
#include<linux/module.h>
#include<linux/init.h>
MODULE_LICENSE("GPL");
static char *name = "WENHUI";
static int age = 30;
module_param(age, int, S_IRUGO); // S_IRUGO 参数读写权限
module_param(name, charp, S_IRUGO); //
static int __init hello_init(void)
{
printk("<0>Name:%s\n",name); //
printk(KERN_INFO "Age:%d\n",age); //
PDEBUG(age);
return 0;
}
static void __exit hello_exit(void)
{
printk("<0> Module exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
KERDIR = /home/linux-2.6.32.2
obj-m += debug_example.o
build: kernel_modules
kernel_modules:
make -C $(KERDIR) M=$(CURDIR) modules
clean:
make -C $(KERDIR) M=$(CURDIR) clean
#Comment/ucomment the following line to disable/enable debugging
DEBUG = n
#add your debugging flag(or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DXXX_DEBUG # "-O"is needed to expand inlines
else
DEBFLAGS = -O2
endif
#CFLAGS += $(DEBFLAGS)
EXTRA_CFLAGS += $(DEBFLAGS)