我对内核模块编程很新,现在我正在尝试运行最基本的hello world模块程序,但是我无法获得任何输出.
我已经编写了Linux设备驱动程序第3版中引入的hello world程序,并从this website和this one获得了一些帮助.
你好ç
#include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("GPL"); static int hello_init(void){ printk("<1>Hello,world!\n"); return 0; } static void hello_exit(void){ printk(KERN_ALERT "Goodbye,world..\n"); } module_init(hello_init); module_exit(hello_exit);
该文件位于/ home / volkan / drive目录中.连同c文件,我有我的Makefile
Makefile文件
obj-m += hello.o
从终端,我执行此命令来编译模块:
sudo make -C /lib/modules/3.8.0-19-generic/build M=/home/volkan/drive/ modules
导致:
make: Entering directory `/usr/src/linux-headers-3.8.0-19-generic' CC [M] /home/volkan/drive/hello.o Building modules,stage 2. MODPOST 1 modules CC /home/volkan/drive/hello.mod.o LD [M] /home/volkan/drive/hello.ko make: Leaving directory `/usr/src/linux-headers-3.8.0-19-generic'
我认为到目前为止,没有任何问题.现在,我插入我的模块,然后删除:
volkan@Varaquilex ~/drive $sudo insmod ./hello.ko volkan@Varaquilex ~/drive $sudo rmmod hello volkan@Varaquilex ~/drive $
解决方法
内核消息记录在/ var / log中的kern.log文件中.根据您的系统,它也可能是dmesg.所以你必须相应地.
使用命令cat /var/log/kern.log
Dec 9 18:51:10 Varaquilex kernel: [ 2818.079572] <1>Hello,world! Dec 9 18:55:02 Varaquilex kernel: [ 3050.256134] Goodbye,world..