內核Debugfs文件系統使用指南

什麼是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

 

 

 

 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章