在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!

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...