Linux debugfs虚拟文件系统

文章目录

简介

环境:Linux dev-PC 5.18.17-amd64-desktop-hwe #20.01.00.10 SMP PREEMPT_DYNAMIC Thu Jun 15 16:17:50 CST 2023 x86_64 GNU/Linux

debugfs是一种用来调试内核的内存文件系统,可以通过debugfs和用户空间交换数据。

debugfs一般挂载:

bash 复制代码
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)

目前环境上debugfs下面有这些节点:

bash 复制代码
ls -lh /sys/kernel/debug
总用量 0
drwxr-xr-x  2 root root 0 2月   9 11:29 acpi
drwxr-xr-x 13 root root 0 2月   9 11:30 bdi
drwxr-xr-x  3 root root 0 2月   9 11:29 binder
drwxr-xr-x 12 root root 0 2月   9 11:29 block
drwxr-xr-x  2 root root 0 2月   9 03:29 bluetooth
--w-------  1 root root 0 2月   9 11:29 clear_warn_once
drwxr-xr-x  2 root root 0 2月   9 11:29 clk
drwxr-xr-x  2 root root 0 2月   9 11:29 devfreq
drwxr-xr-x  2 root root 0 2月   9 11:29 device_component
-r--r--r--  1 root root 0 2月   9 11:29 devices_deferred
drwxr-xr-x  2 root root 0 2月   9 11:29 dma_buf
drwxr-xr-x  2 root root 0 2月   9 11:29 dmaengine
drwxr-xr-x  2 root root 0 2月   9 11:29 dma_pools
drwxr-xr-x  3 root root 0 2月   9 11:29 dri
drwxr-xr-x  2 root root 0 2月   9 11:29 dynamic_debug
drwxr-xr-x  2 root root 0 2月   9 11:29 error_injection
drwxr-xr-x  2 root root 0 2月   9 11:29 extfrag
-rw-r--r--  1 root root 0 2月   9 11:29 fault_around_bytes
drwxr-xr-x  2 root root 0 2月   9 11:29 frontswap
-r--r--r--  1 root root 0 2月   9 11:29 gpio
drwxr-xr-x  3 root root 0 2月   9 11:29 hid
drwxr-xr-x  2 root root 0 2月   9 03:29 ieee80211
drwxr-xr-x  2 root root 0 2月   9 11:29 iosf_sb
drwxr-xr-x  2 root root 0 2月   9 11:29 kprobes
drwxr-xr-x  2 root root 0 2月   9 03:29 kvm
-rw-r--r--  1 root root 0 2月   9 11:29 lru_gen
-r--r--r--  1 root root 0 2月   9 11:29 lru_gen_full
drwxr-xr-x  2 root root 0 2月   9 11:29 mce
drwxr-xr-x  2 root root 0 2月   9 11:29 opp
drwxr-xr-x  2 root root 0 2月   9 11:29 pinctrl
drwxr-xr-x  2 root root 0 2月   9 11:29 pmc_core
drwxr-xr-x  2 root root 0 2月   9 11:29 pm_genpd
-r--r--r--  1 root root 0 2月   9 11:29 pwm
drwxr-xr-x  3 root root 0 2月   9 11:29 ras
drwxr-xr-x  2 root root 0 2月   9 11:29 regmap
drwxr-xr-x  3 root root 0 2月   9 11:29 regulator
drwxr-xr-x  2 root root 0 2月   9 11:29 remoteproc
drwxr-xr-x  4 root root 0 2月   9 11:29 sched
drwxr-xr-x  2 root root 0 2月   9 11:29 slab
-r--r--r--  1 root root 0 2月   9 11:29 sleep_time
--w-------  1 root root 0 2月   9 11:29 split_huge_pages
-r--r--r--  1 root root 0 2月   9 11:29 suspend_stats
drwxr-xr-x  2 root root 0 2月   9 11:29 swiotlb
drwxr-xr-x  2 root root 0 2月   9 11:29 sync
dr-xr-xr-x  3 root root 0 2月   9 11:29 tracing
drwxr-xr-x  2 root root 0 2月   9 11:29 ttm
drwxr-xr-x  6 root root 0 2月   9 11:29 usb
drwxr-xr-x  2 root root 0 2月   9 11:29 virtio-ports
-r--r--r--  1 root root 0 2月   9 11:29 wakeup_sources
drwxr-xr-x  3 root root 0 2月   9 11:29 x86
drwxr-xr-x  2 root root 0 2月   9 11:29 zswap

debugfs接口

debugfs代码在fs/debugfs下面,提供的接口定义在include/linux/debugfs.h

以suspend_stats文件为例简单看下

kernel/power/main.c中查看初始化函数:

c 复制代码
static int __init pm_debugfs_init(void)
{
	debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
			NULL, NULL, &suspend_stats_fops);
	return 0;
}

通过debugfs_create_file在debugfs文件系统中创建一个名为suspend_stats的文件,其他参数解释如下:

c 复制代码
/**
 * debugfs_create_file - create a file in the debugfs filesystem
 * @name: a pointer to a string containing the name of the file to create.
 * @mode: the permission that the file should have.
 * @parent: a pointer to the parent dentry for this file.  This should be a
 *          directory dentry if set.  If this parameter is NULL, then the
 *          file will be created in the root of the debugfs filesystem.
 * @data: a pointer to something that the caller will want to get to later
 *        on.  The inode.i_private pointer will point to this value on
 *        the open() call.
 * @fops: a pointer to a struct file_operations that should be used for
 *        this file.
 */
struct dentry *debugfs_create_file(const char *name, umode_t mode,
				   struct dentry *parent, void *data,
				   const struct file_operations *fops);

通过suspend_stats_fops来定义该文件的读操作。

参考

DebugFS

相关推荐
一位摩羯座DBA1 小时前
Redhat&Centos挂载镜像
linux·运维·centos
学习3人组1 小时前
CentOS配置网络
linux·网络·centos
weixin_307779131 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
漫步企鹅2 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win2 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、2 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
冰橙子id3 小时前
linux系统安全
linux·安全·系统安全
stark张宇3 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao3 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
悲伤小伞3 小时前
linux_git的使用
linux·c语言·c++·git