c - 为什么我的 hello world 驱动程序模块不打印任何内容?

标签 c linux-device-driver kernel-module

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

我编写了 Linux 设备驱动程序第 3 版中介绍的 hello world 程序,并从 this website 获得了一些帮助和 this one .

你好.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 方面也没有什么经验,所以非常欢迎解释性答案。难道我做错了什么?为什么我看不到任何输出?

最佳答案

内核消息记录在位于 /var/logkern.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..

关于c - 为什么我的 hello world 驱动程序模块不打印任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20480291/

相关文章:

linux - pci_Driver.probe 未被调用

linux - IOCTL Linux 设备驱动程序

c - 使用 writel 将 4 位写入 ioremap 内存地址

linux - 在内核中添加新的 IOCTL(数字范围)

c - "expected'; ',' 或 'or' ) 'before' * 'token"使用结构时出错

c - 此代码的预期输出

c++ - 插入排序C实现

linux-kernel - 持有自旋锁自动返回是不安全的吗?

Linux 内核 : CMA & Device Tree

c++ - 这个makefile有什么问题