gpt4 book ai didi

c - 使用 insmod 插入模块后没有输出到终端

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:37 27 4
gpt4 key购买 nike

我正在关注以下 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 命令后,终端会打印"Hello world!""Goodbye,残酷的世界!”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

谢谢。

最佳答案

kprintf 的输出总是在内核日志中。这也可能碰巧转到系统控制台(因此,如果您在控制台上,则转到您正在使用的终端),但是没有任何东西可以将它链接回您加载模块的特定终端。

要读取内核日志,运行dmesg

关于c - 使用 insmod 插入模块后没有输出到终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10679500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com