Linux驱动 | debugfs接口创立
点击上方“嵌入式Linux充电站”,选择“置顶/星标民众号”
福利干货,第一时间送达
上篇介绍了procfs接口的创立,今天再介绍一种debugfs接口的创立。
实现效果
在/sys/kernel/debug/目录下创立一个ion/test文件,通过cat、echo的方法进行读写操作:
前期准备
内核配置翻开debugfs:
CONFIG_DEBUG_FS=y
登录后复制
挂载debugfs文件系统:
mount -t debugfs none /sys/kernel/debug
登录后复制
代码实现
读写变量:
#include <linux/debugfs.h> #include <linux/module.h> #include <linux/types.h> static struct dentry *ion_dir; static u64 test_u64 = 0; static int __init debugfs_init(void) { //创立一个/sys/kernel/debug/ion目录 ion_dir = debugfs_create_dir("ion", NULL); if (!ion_dir) { printk("ion_dir is nulln"); return -1; } /* 创立/sys/kernel/debug/ion/test_u64文件 */ debugfs_create_u64("test_u64", 0644, ion_dir, &test_u64); return 0; } static void __exit debugfs_exit(void) { debugfs_remove_recursive(ion_dir); } module_init(debugfs_init); module_exit(debugfs_exit); MODULE_LICENSE("GPL");
登录后复制
运行结果:
读写字符串:
#include <linux/debugfs.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/errno.h> #include <linux/dcache.h> #include <linux/types.h> static char ion_buf[512] = "hellon"; static struct dentry *ion_dir; static int ion_open(struct inode *inode, struct file *filp) { //printk("ion openn"); return 0; } ssize_t ion_read(struct file *filp, char __user *buf, size_t count, loff_t *offp) { int retval = 0; if ((*offp + count) > 512) count = 512 - *offp; if (copy_to_user(buf, ion_buf+*offp, count)) { printk("copy to user failed, count:%ldn", count); retval = -EFAULT; goto out; } *offp += count; retval = count; out: return retval; } ssize_t ion_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp) { int retval; if (*offp > 512) return 0; if (*offp + count > 512) count = 512 - *offp; if (copy_from_user(ion_buf+*offp, buff, count)) { printk("copy from user failed, count:%ldn", count); retval = -EFAULT; goto out; } *offp += count; retval = count; out: return retval; } struct file_operations my_fops = { .owner = THIS_MODULE, .read = ion_read, .write = ion_write, .open = ion_open, }; static int __init debugfs_init(void) { printk("INIT MODULEn"); //创立一个/sys/kernel/debug/ion目录 ion_dir = debugfs_create_dir("ion", NULL); if (!ion_dir) { printk("ion_dir is nulln"); return -1; } /* 创立/sys/kernel/debug/ion/test文件 */ struct dentry *filent = debugfs_create_file("test", 0644, ion_dir, NULL, &my_fops); if (!filent) { printk("test file is nulln"); return -1; } return 0; } static void __exit debugfs_exit(void) { debugfs_remove_recursive(ion_dir); } module_init(debugfs_init); module_exit(debugfs_exit); MODULE_LICENSE("GPL");
登录后复制
运行结果:
函数接口说明
创立目录、文件函数:
/* 创立目录 */ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); /*创立节点 */ struct dentry *debugfs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops);
登录后复制
name:要创立的/sys/kernel/debug下的目录名
parent:父目录,用struct dentry结构体体现。如果直接在/sys/kernel/debug/下创立文件,则为NULL
创立差别巨细的文件:
//创立十进制的无符号文件 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, u8 *value); void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, u16 *value); void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, u32 *value); void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, u64 *value); //创立十六进制的无符号文件 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, u8 *value); void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, u16 *value); void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, u32 *value); void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, u64 *value);
登录后复制
更详细的debugfs用法请参考官方文档:Documentation/filesystems/debugfs.txt
以上就是Linux驱动 | debugfs接口创立的详细内容,更多请关注本网内其它相关文章!
免责申明:以上展示内容来源于相助媒体、企业机构、网友提供或网络收集整理,版权争议与本站无关,文章涉及见解与看法不代表公海彩船滤油机网官方立场,请读者仅做参考。本文接待转载,转载请说明来由。若您认为本文侵犯了您的版权信息,或您发明该内容有任何涉及有违公德、冒犯执法等违法信息,请您立即联系公海彩船实时修正或删除。