/*模塊加載是向內核傳遞參數 如果不傳遞就用默認的參數*/
/*用戶向內核傳遞參數的方法 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)