我正在关注以下tutorial,试图学习如何开发设备驱动程序,在第2章中,重点是开发一个工作模块并将其插入到内核中 . 我使用了以下代码(hello.c):
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
我的这是Makefile:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
然后我在LXTerminal中运行以下命令:
brian@brian-desktop:~/driver_stuff/hello$ su
root@brian-desktop:/home/brian/driver_stuff/hello# make
make -C /lib/modules/2.6.32-21-generic/build M=/home/brian/driver_stuff/hello modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-21-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-21-generic'
root@brian-desktop:/home/brian/driver_stuff/hello# insmod ./hello.ko
root@brian-desktop:/home/brian/driver_stuff/hello# rmmod hello
root@brian-desktop:/home/brian/driver_stuff/hello# exit
但是,在 insmod ./hello.ko 命令之后,应该期望终端在 rmmod hello 命令之后打印 "Hello world!" 然后 "Goodbye, cruel world!" . 书中提到,当你在控制台中运行命令时会发生这种情况,但是在模拟终端中却没有,这可能是问题吗?
我还检查了/ var / log / messages和/var/log/messages.1,它们没有“Hello World!”的记录 . 也不是“再见,残酷的世界!” . 是否可能这些消息位于不同的文件中,或者是消息首先没有被推送到内核的问题?
如果您需要有关我正在运行的内核的信息(Lubuntu 10.04,在VM中):
brian@brian-desktop:~/driver_stuff/hello$ uname -r
2.6.32-21-generic
谢谢 .
1 回答
kprintf
的输出始终是内核日志 . 这可能也会发生在系统控制台上(因此也可能发送到控制台上的终端),但没有任何东西可以将它链接回您加载模块的特定终端 .要读取内核日志,请运行
dmesg
.