Linux之debugfs介绍
来源:互联网 发布:hbuilder for mac下载 编辑:程序博客网 时间:2024/04/20 11:52
Linux之debugfs介绍
Debugfs是一种用于内核调试的虚拟文件系统,在内核源码中经常可以看到它的使用,今天我来大概介绍一下它的使用。
如果你对debugfs不熟悉,那么也许你会对sysfs比较熟悉,对于调试方面,其实两个文件系统还是挺类似的,但是sysfs的引入内核主要是用于驱动模型的。所以我们在平时调试的时候应该尽量避免使用sysfs,而使用debugfs。
好了,下面我们来介绍一些debugfs的使用,要使用debugfs首先我们要设置一下配置选项CONFIG_DEBUG_FS,可以在config文件中设置CONFIG_DEBUG_FS=y,也可以通过menuconfig来设置,如图:
- Kernelhacking --->
- [*]Debug Filesystem
驱动中使用debugfs需要包含头文件<linux/debugfs.h>,为了在用户态下使用debugfs,必须把它mount到一个目录下,我们可以把它放在mnt目录下。
使用如下命令:
- <span xmlns="http://www.w3.org/1999/xhtml" style="">mount-t debugfs none /mnt</span>
然后进入/mnt后就可以看到我们在系统中创建的这些文件。
下面我们开始说一下如何在驱动中使用debugfs.
首先我们需要创建一个自己的目录,利用如下函数:
- <span xmlns="http://www.w3.org/1999/xhtml" style="">struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);</span>
name就是创建的目录的名字,parent是该目录的父目录,如果是NULL的话,则所创建的目录就在debugfs的根目录,具体使用如下:
- static struct dentry *binder_debugfs_dir_entry_root;
- binder_debugfs_dir_entry_root= debugfs_create_dir("binder", NULL);
这样就会在debugfs的根目录下创建一个binder的目录,有了目录还需要有可供读写的文件吧,下边就是另一个重要的函数,文件的创建:
- struct dentry *debugfs_create_file(const char *name, mode_t mode,
- struct dentry *parent, void *data,
- const struct file_operations *fops)
如其函数名,这个函数的作用就是在parent这个目录下创建一个名为name的文件,mode是这个文件读写权限,data是传入的参数,fops就比较重要了,为我们的文件提供实际的读写操作。
在binder驱动中创建了如下文件
- debugfs_create_file("state",
- S_IRUGO,
- binder_debugfs_dir_entry_root,
- NULL,
- &binder_state_fops);
- debugfs_create_file("stats",
- S_IRUGO,
- binder_debugfs_dir_entry_root,
- NULL,
- &binder_stats_fops);
- debugfs_create_file("transactions",
- S_IRUGO,
- binder_debugfs_dir_entry_root,
- NULL,
- &binder_transactions_fops);
- debugfs_create_file("transaction_log",
- S_IRUGO,
- binder_debugfs_dir_entry_root,
- &binder_transaction_log,
- &binder_transaction_log_fops);
- debugfs_create_file("failed_transaction_log",
- S_IRUGO,
- binder_debugfs_dir_entry_root,
- &binder_transaction_log_failed,
- &binder_transaction_log_fops);
如上图所示,在binder目录下创建了proc/state/stats/transactions/transaction_log/failed_transaction_log这些文件。
在binder中这些文件的fops全部用一个宏来完成了
- #define BINDER_DEBUG_ENTRY(name) \
- static int binder_##name##_open(struct inode *inode, struct file *file) \
- {\
- return single_open(file, binder_##name##_show, inode->i_private); \
- }\
- \
- static const struct file_operations binder_##name##_fops = { \
- .owner= THIS_MODULE, \
- .open= binder_##name##_open, \
- .read= seq_read, \
- .llseek= seq_lseek, \
- .release= single_release, \
- }
可以看到binder没有实现write操作。read则全部使用seq_file的操作,seq_file是2.4.15以后版本的内核中出现的新功能,使得内核输出大文件信息。这边我们就不去将seq了,我们放在另外的一篇文章里面去讲。
debugfs除了实现这个文件对函数的功能,还提供了一些API用来针对变量的操作:
- struct dentry *debugfs_create_u8(const char *name, mode_t mode, struct dentry *parent, u8 *value);
- struct dentry *debugfs_create_u16(const char *name, mode_t mode, structdentry *parent, u16 *value);
- struct dentry *debugfs_create_u32(const char *name, mode_t mode, structdentry *parent, u32 *value);
- struct dentry *debugfs_create_bool(const char *name, mode_t mode, structdentry *parent, u32 *value);
这样就可以在用户态去调试内核变量value了。
当内核卸载时,debugfs并不会自动的去清除我们创建的这些文件,对于创建的每一个文件我们都需要调用如下函数接口去清除:
- Linux之debugfs介绍
- Linux之debugfs介绍
- Linux之debugfs介绍
- linux debug 之- debugfs
- Linux Debugfs文件系统介绍and 使用
- Linux Kernel —— DebugFS介绍
- linux debugfs
- linux debugfs
- Linux内核空间-用户空间通信之debugfs
- Linux之DebugFS(一)-----------------------追踪内核函数调用
- mount debugfs on Linux
- linux的debugfs
- linux debugfs 恢复文件
- linux内核 DebugFS
- linux中用debugfs调试
- linux内核 DebugFS
- debugfs, tmpfs, proc, sysfs 介绍
- Debugfs
- 架构师应该掌握什么技术?
- selenim java判断图片有没有加载完成
- 华为OJ(名字漂亮度)
- 关于android中broadcast和BroadcastReceiver浅谈
- [LeetCode] Populating Next Right Pointers in Each Node II
- Linux之debugfs介绍
- POJ 2528 Mayor's posters (线段树+离散化)
- ADNROID XML图像资源文件详细讲解(五)
- bzoj 1199 二分暴力
- 将 res 资源文件转换成 file
- C/C++语言 void 及 void 指针深层探索
- ftp上传图片的简单用法
- aapt简介及常用命令
- 简单实现类似Spring的在任意代码中获取Request的功能