微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ubuntu上开发编译内核模块,并查看printk打印的消息

模块源码:

// 下面的是主要的内容
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>


MODULE_LICENSE("GPL");


static int year=2017;


int hello_init()
{
printk(KERN_WARNING "Hello kernel,it's %d!\n",year);
return 0;
}




void hello_exit()
{
printk("Bye,kernel!\n");
}


// 下面两个为关键的模块函数
module_init(hello_init);
module_exit(hello_exit);

Makefile:

ifneq ($(KERNELRELEASE),)


obj-m := helloworld.o


else


KDIR=/lib/modules/3.5.0-23-generic/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.c *.symvers
endif

执行makefile编译模块-》执行insmod helloworld.ko控制台不会显示printk打印的消息,应该使用 dmesg -c指令。成功看到打印的消息

root@ubuntu:/home/gec/driver# dmesg -c
[706893.639845] Hello kernel,it's 2017!

原文地址:https://www.jb51.cc/ubuntu/351660.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐