Linux 内核debugfs总结

Debugfs 是内核开发人员向用户空间提供信息的一种简单方法。/proc 仅用于提供有关进程的信息,或者 sysfs 具有严格的每个文件一个值的规则,而 debugfs 则完全没有规则。开发人员可以将他们想要的任何信息放在那里。debugfs 文件系统也不能作为用户空间的稳定 ABI;理论上,在那里导出的文件没有稳定性限制。现实世界并不总是那么简单,即使是 debugfs 接口,最好的设计也是考虑到它们需要永远维护。(或等效的 /etc/fstab 行)。
摘要由CSDN通过智能技术生成

最近用到debugfs这个东西,网上找了一堆资料,希望看完这一篇够了。

前言

内核开发者经常需要导出一些信息到用户空间,用于分析内核运行逻辑。最常见的方法是使用 printk(),不过在嵌入式中,printk() 往往直接打印到 console,一旦 printk() 被频繁调用的话,console 就会被刷屏,此时输入命令都是件困难的事情。
有时我们只想偶尔看一下某个内核变量的值,但是一旦使用 printk(),它就会无休止地循环打印;另一方面,使用 printk() 只能打印,而不能从用户空间去修改内核变量的值。为了应对这种情况,我们可以使用 procfs 和 sysfs 这两个虚拟文件系统来实现上述需求。不过通过 procfs 和 sysfs 创建一个文件,来读写某个变量的值,从编码角度看,略微复杂了些。
debugfs

为了让开发人员更轻松地实现调试,内核提供了 debugfs,这是一个致力于调试信息的虚拟文件系统。debugfs 旨在成为一个相对简单和轻量级的子系统。开发人员只需要寥寥几行代码就可以实现调试,并且只需要引用一个头文件 linux/debugfs.h。
示例(调试内核里面一个 u32 类型的变量 count):

debugfs_int.c

#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>

static struct dentry *hello_root;
static u32 count;

static int __init hello_init(void)
{
    hello_root = debugfs_create_dir("hello", NULL);
    if (hello_root == NULL) {
        printk("%s: create debugfs dir failed\n", __func__);
        return -1;
    }

    count = 100;
    debugfs_create_u32("count", 0644, hello_root, &count);

    return 0;
}

static void __exit hello_exit(void)
{
    if (hello_root) {
        debugfs_remove_recursive(hello_root);
    }
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");

Makefile


KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

obj-m    :=debugfs_int.o

all:
    make -C $(KERNELDIR) M=$(PWD) modules

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.* .tmp_versions *.mod *.order *.symvers *.dwo


   

# make && insmod debugfs_int.ko
# cat /sys/kernel/debug/hello/count
100
# echo 30 > /sys/kernel/debug/hello/count
# cat /sys/kernel/debug/hello/count
30

  

使用起来是不是非常方便,除了u32,debugfs 还有很多类型接口

debugfs_create_u8()
debugfs_create_u16()
debugfs_create_u64()
debugfs_create_x8()
debugfs_create_x16()
...
debugfs_create_size_t()
debugfs_create_bool()
...

 

debugfs_create_file()

如果想一次性打印多个变量的值,可以使用 debugfs_create_file(),它可以自定义 read() 函数,我们可以在其中输入多个变量的值。
示例:
debugfs_file.c

#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>

static struct dentry *hello_root;

int count_1 = 20;
int count_2 = 30;

static ssize_t hello_read(struct file *filp, char __user *user_buf, size_t count, loff_t *ppos)
{
    char *buf;
    u32 len = 0, size = 4096;
    size_t retval;

    buf = kzalloc(size, GFP_KERNEL);
    if (buf == NULL)
        return -ENOMEM;

    len = scnprintf(buf, size, "count_1: %d\n", count_1);
    len += scnprintf(buf + len, size - len, "count_2: %d\n", count_2);

    retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
    kfree(buf);

    return retval;
}

static ssize_t hello_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
    return count;
}

static struct file_operations hello_fops = {
    .owner = THIS_MODULE,
    .read = hello_read,
    .write = hello_write,
};

static int __init hello_init(void)
{
    hello_root = debugfs_create_dir("hello", NULL);
    if (hello_root == NULL) {
        printk("%s: create debugfs dir failed\n", __func__);
        return -1;
    }

    debugfs_create_file("hello_file", S_IWUGO, hello_root, NULL, &hello_fops);

    return 0;
}

static void __exit hello_exit(void)
{
    if (hello_root) {
        debugfs_remove_recursive(hello_root);
    }
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");

   
  
 


KERNELDIR ?= /lib/modules/$(shell uname -r)/build

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值