深入解析内核Debugfs文件系统应用指南
最编程
2024-08-13 21:23:50
...
什么是Debugfs文件系统?
linux文件系统fs的一种,在kernel space和user space之间传递数据,主要是kernel向user输出debug信息。
与sysfs和procfs的区别
procfs:主要输出系统内核参数和进程信息,目录位于/proc/*,参考http://blog.chinaunix.net/uid-20543672-id-3220151.html
sysfs:linux设备模型,根据类型对设备进行分类,目录位于/sys/*,参考https://blog.****.net/new_abc/article/details/7555610
Debugfs的常用数据结构和函数
debugfs的基本实现方式类似字符设备驱动,通过一个file_operation结构体,源码参考<linux/debugfs.h>
static const struct file_operations __fops = { \
.owner = THIS_MODULE, \
.open = __fops ## _open, \
.release = simple_attr_release, \
.read = debugfs_attr_read, \
.write = debugfs_attr_write, \
.llseek = no_llseek, \
}
一、创建目录(可省略)
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
该函数会在parent目录下创建名为name的目录,如果parent为空,则在/sys/kernel/debug/下。
创建成功后函数返回一个dentry结构体,用于创建和清理debug file。
dentry结构体参数
struct dentry {
/* RCU lookup touched fields */
unsigned int d_flags; /* protected by d_lock */
seqcount_t d_seq; /* per dentry seqlock */
struct hlist_bl_node d_hash; /* lookup hash list */
struct dentry *d_parent; /* parent directory */
struct qstr d_name;
struct inode *d_inode; /* Where the name belongs to - NULL is
* negative */
unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */
/* Ref lookup also touches following */
struct lockref d_lockref; /* per-dentry lock and refcount */
const struct dentry_operations *d_op;
struct super_block *d_sb; /* The root of the dentry tree */
unsigned long d_time; /* used by d_revalidate */
void *d_fsdata; /* fs-specific data */
union {
struct list_head d_lru; /* LRU list */
wait_queue_head_t *d_wait; /* in-lookup ones only */
};
struct list_head d_child; /* child of parent list */
struct list_head d_subdirs; /* our children */
/*
* d_alias and d_rcu can share memory
*/
union {
struct hlist_node d_alias; /* inode alias list */
struct hlist_bl_node d_in_lookup_hash; /* only for in-lookup ones */
struct rcu_head d_rcu;
} d_u;
} __randomize_layout;
二、创建debugfs file
struct dentry *debugfs_create_file(const char *name, umode_t mode,
struct dentry *parent, void *data,
const struct file_operations *fops);
- name:文件名
- mode:文件的访问权限
- parent:在哪个目录下创建,即上步创建目录返回的dentry结构体
- data:数据储存在struct dentry中structinode的i_private中
- fops:文件操作函数,一般采用seq_file
创建确定初始大小的file
struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
struct dentry *parent, void *data,
const struct file_operations *fops,
loff_t file_size);
- file_size:文件大小
创建固定长度的file
struct dentry *debugfs_create_u8(const char *name, umode_t mode,
struct dentry *parent, u8 *value);
struct dentry *debugfs_create_u16(const char *name, umode_t mode,
struct dentry *parent, u16 *value);
struct dentry *debugfs_create_u32(const char *name, umode_t mode,
struct dentry *parent, u32 *value);
struct dentry *debugfs_create_u64(const char *name, umode_t mode,
struct dentry *parent, u64 *value);
- 文件只包含一个整数,单值文件
十六进制文件
struct dentry *debugfs_create_x8(const char *name, umode_t mode,
struct dentry *parent, u8 *value);
struct dentry *debugfs_create_x16(const char *name, umode_t mode,
struct dentry *parent, u16 *value);
struct dentry *debugfs_create_x32(const char *name, umode_t mode,
struct dentry *parent, u32 *value);
struct dentry *debugfs_create_x64(const char *name, umode_t mode,
struct dentry *parent, u64 *value);
布尔值文件
struct dentry *debugfs_create_bool(const char *name, umode_t mode,
struct dentry *parent, bool *value);
另外还能创建包括atomic_t,二进制等类型的file,详见<linux/debugfs.h>
三、删除debugfs file
void debugfs_remove(struct dentry *dentry);
void debugfs_remove_recursive(struct dentry *dentry);
后者可以递归地删除顶层dentry下的所有file。
内容参考linux-4.19.47\Documentation\filesystems\debugfs.txt
下一篇: 使用debugfs:Linux调试的指南
推荐阅读
-
深入解析内核Debugfs文件系统应用指南
-
【2022新手指南】Java编程进阶之路 - 六、技术架构篇 ### MySQL索引底层解析与优化实战 - 你会讲解MySQL索引的数据结构吗?性能调优技巧知多少? - Redis深度揭秘:你知道多少?从基础到哨兵、主从复制全梳理 - Redis持久化及哨兵模式详解,还有集群搭建和Leader选举黑箱打开 - Zookeeper是个啥?特性和应用场景大公开 - ZooKeeper集群搭建攻略及 Leader选举、读写一致性、共享锁实现细节 - 探究ZooKeeper中的Leader选举机制及其在分布式环境中的作用 - Zab协议深入剖析:原理、功能与在Zookeeper中的核心地位 - RabbitMQ全方位解读:工作模式、消费限流、可靠投递与配置策略 - 设计者视角:RabbitMQ过期时间、死信队列与延时队列实践指南 - RocketMQ特性和应用场景揭示:理解其精髓与差异化优势 - Kafka详细介绍:特性及广泛应用于实时数据处理的场景解析 - ElasticSearch实力揭秘:特性概述与作为搜索引擎的广泛应用 - MongoDB认知升级:非关系型数据库的优势阐述,安装与使用实战教学 - BIO/NIO/AIO网络模型对比:掌握它们的区别与在网络编程中的实际应用 - Netty带你飞:理解其超快速度背后的秘密,包括线程模型分析 - 网络通信黑科技:Netty编解码原理与常用编解码器的应用,Protostuff实战演示 - 解密Netty粘包与拆包现象,怎样有效应对这一常见问题 - 自定义Netty心跳检测机制,轻松调整检测间隔时间的艺术 - Dubbo轻骑兵介绍:核心特性概览,服务降级实战与其实现益处 - Dubbo三大神器解读:本地存根与本地伪装的实战运用与优势呈现 ----------------------- 七、结语与回顾
-
玩转Kotlin性能测试:JMH入门指南一 - 测试基础" "深入理解JMH在Kotlin中的应用:基准测试实战解析" "轻松实践Kotlin基准测试:JMH工具详解与实例总结
-
深入理解Linux指令 | tar命令实操指南:打包与解压文件的艺术,广泛应用于备份与文件压缩——第二部分,操作技巧解析
-
深度学习中的不确定性量化:2020年实用技术与应用大解析 - 61页精华解读" 这份报告深入剖析了近年来深度学习领域中不确定性量化(UQ)技术的最新发展,包括其在强化学习(RL)中的运用实例。探讨了贝叶斯近似和集成学习等主流UQ方法在各个具体场景中的广泛应用,比如自动驾驶、目标识别、图像修复、医疗影像分析(如分类和分割)、文本理解(如文本分类和风险评估)、以及生物信息学等多个领域。 报告进一步梳理了UQ方法在深度学习领域的关键应用案例,并针对当前面临的挑战及未来研究方向进行了概览和展望,为这一领域的研究人员和实践者提供了有价值的参考指南。
-
数仓建设实战指南:ODS、DWD和DWM的深入解析与应用
-
实操指南:深入解析JVM调优 - jmap、jstack、jstat在实际项目中的应用
-
深入浅出-Zabbix 5.0最新稳定版解析8:玩转Zabbix监控Java应用实战(包括JMX与Zabbix-Java-Gateway实操、详析Java项目监控流程及Tomcat部署启动指南)
-
全面指南:Pytest与Allure测试框架的深入解析及实战应用
-
深入解析Linux内核中的根文件系统挂载流程