什么是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.csdn.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