debugfs

108 篇文章 24 订阅
48 篇文章 6 订阅

前言

内核开发者经常需要导出一些信息到用户空间,用于分析内核运行逻辑。最常见的方法是使用 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
PWD       := $(shell pwd)

obj-m	:=debugfs_file.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_file.ko
# cat /sys/kernel/debug/hello/hello_file 
count_1: 20
count_2: 30
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li-Yongjun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值