1. 前言
Debugfs虚拟文档系统是一种内核空间与用户空间的接口,基于libfs库实现,专用于开发人员调试,便于向用户空间导出内核空间数据。
内核开发者经常需要向用户空间应用输出一些调试信息,在稳定的系统中可能根本不需要这些调试信息,但是在开发过程中,为了搞清楚内核的行为,调试信息非常必要。
2. 使用
2.1 自动挂载
要使用debugfs,需要在内核编译配置中配置 CONFIG_DEBUG_FS=y选项,并且将其自动挂载到默认的目录 /sys/kernel/debug。
2.2 手动挂载
mkdir tmp //新建目录(手动挂载点)
mount -t debugfs debugfs tmp //挂载到tmp/ 目录
3. Debugfs的API
//创建和撤销目录及文件
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
struct dentry *debugfs_create_file(const char *name, mode_t mode, struct dentry *parent, void *data, const struct file_operations *fops);
void debugfs_remove(struct dentry *dentry);
void debugfs_remove_recursive(struct dentry *dentry);
//······
4. 应用
4.1 查看GPIO使用情况
在实际的开发任务中,有时候不知道GPIO的状态,也不知道在内核中GPIO是否申请成功。
可以通过/sys/kernel/debug/gpio这个文件来查看。这个文件显示了申请成功的GPIO的输入输出状态和电平。
//1.去默认挂载点查看gpio的使用情况
cat sys/kernel/debug/gpio
//2.去手动挂载点(/system/tmp)查看gpio的使用情况
mount -rw -o remount /system //创建tmp目录时提示文件系统只读时重新mount一下该目录
cd system
mkdir tmp //新建目录(手动挂载点)
mount -t debugfs debugfs tmp/ //挂载到/system/tmp 目录
cat /system/tmp/gpio
//cat 之后就会显示gpio的使用情况
GPIOs 0-31, platform/209c000.gpio, 209c000.gpio:
gpio-1 (2190000.usdhc cd ) in lo
gpio-26 (sysfs ) in hi
GPIOs 32-63, platform/20a0000.gpio, 20a0000.gpio:
gpio-34 (OTG_CHARGE_EN ) out lo
gpio-35 (otg-mode ) out lo
GPIOs 64-95, platform/20a4000.gpio, 20a4000.gpio:
gpio-84 (spi_imx ) out lo
GPIOs 96-127, platform/20a8000.gpio, 20a8000.gpio:
GPIOs 128-159, platform/20ac000.gpio, 20ac000.gpio:
······