【发布时间】:2013-12-10 05:03:07
【问题描述】:
我对内核模块编程非常陌生,现在我正在尝试运行最基本的 hello world 模块程序,但是我无法获得任何输出。
我已经编写了 Linux Device Drivers 3rd ed 中介绍的 hello world 程序,并得到了 this website 和 this one 的一些帮助。
hello.c
#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
生成文件
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 $
没有输出。我在 linux 方面也没有什么经验,所以非常欢迎解释性的答案。难道我做错了什么?为什么我看不到任何输出?
【问题讨论】:
-
dmesg有输出吗? -
当我
cat dmesg看到很多东西,但与模块无关。我认为它应该在文件的末尾,但不仅不在末尾,也不在中间或开头。 -
然而,
cat kern.log成功了:` 12 月 9 日 18:42:46 Varaquilex 内核:[2314.937815] 你好,世界! 12 月 9 日 18:42:49 Varaquilex 内核:[2317.873400] 再见,世界.. `
标签: c linux-device-driver kernel-module