为什么我的hello world驱动模块没有打印任何东西?

Var*_*lex 5 c kernel-module linux-device-driver

我对内核模块编程很新,现在我正在尝试运行最基本的hello world模块程序,但是我无法获得任何输出.

我已经编写了Linux设备驱动程序第3版中介绍的hello world程序,并从本网站网站获得了一些帮助.

你好ç

#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);
Run Code Online (Sandbox Code Playgroud)

该文件在/home/volkan/drive目录中.连同c文件,我有我的Makefile

Makefile文件

obj-m += hello.o
Run Code Online (Sandbox Code Playgroud)

从终端,我执行此命令来编译模块:

sudo make -C /lib/modules/3.8.0-19-generic/build M=/home/volkan/drive/ modules
Run Code Online (Sandbox Code Playgroud)

导致:

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'
Run Code Online (Sandbox Code Playgroud)

我认为到目前为止,没有任何问题.现在,我插入我的模块,然后删除:

volkan@Varaquilex ~/drive $ sudo insmod ./hello.ko
volkan@Varaquilex ~/drive $ sudo rmmod hello
volkan@Varaquilex ~/drive $ 
Run Code Online (Sandbox Code Playgroud)

没有输出.我也没什么经验,所以解释性答案非常受欢迎.难道我做错了什么?为什么我看不到任何输出?

Var*_*lex 5

内核消息记录在kern.log位于 的文件中/var/log。根据您的系统,它也可能位于dmesg. 所以你必须cat相应地进行。

使用命令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..
Run Code Online (Sandbox Code Playgroud)

  • 请注意,它们*不一定*在那里;这是在“/etc/syslog.conf”中确定的(但首先查找“/etc/rsyslog.conf”,如果存在则优先)。您会在该文件中找到一些对“kern.log”的引用。您还会注意到那里使用了优先级(1 = 警报等)。奇怪的是,这并没有以“dmesg”结束。 (2认同)