插入带有insmod的模块后没有输出到终端

0 c linux module kernel-module linux-kernel

我正在按照以下教程,尝试学习如何开发设备驱动程序,在第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);
Run Code Online (Sandbox Code Playgroud)

我的这是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
Run Code Online (Sandbox Code Playgroud)

然后我在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
Run Code Online (Sandbox Code Playgroud)

但是,在insmod ./hello.ko命令之后,人们应该期望终端会打印"Hello world!".然后是"再见,残酷的世界!" rmmod hello命令之后.书中提到,当你在控制台中运行命令时会发生这种情况,而不是在模拟终端中,这可能是问题吗?

我还检查了/ 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
Run Code Online (Sandbox Code Playgroud)

谢谢.

dus*_*uff 5

输出kprintf始终是内核日志.这可能也会发生在系统控制台上(因此,如果你在控制台上,也会转到你正在使用的终端),但没有什么可以将它链接回你加载模块的特定终端.

要读取内核日志,请运行dmesg.