debugfs

Posted Li-Yongjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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

以上是关于debugfs的主要内容,如果未能解决你的问题,请参考以下文章

debugfs使用指南

Linux内核开启DebugFS以及查看debug信息(/sys/kernel/debug及debugfs)

Linux内核开启DebugFS以及查看debug信息(/sys/kernel/debug及debugfs)

Linux用户与内核空间交互—debugfs

Linux用户与内核空间交互—debugfs

Linux驱动调试中的Debugfs的使用简介