#############
基于Linux3.10
#############
下例关于Linux下debugfs文件的创建及读写
/*===============================================================
* Copyright (C) 2015 All rights reserved.
*
* File :debugfs.c
* Author:Gavin (gavinlu1015@gmail.com)
* Date :2015-04-22
* Remark:基于linux3.10
*
================================================================*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/debugfs.h>
MODULE_LICENSE("GPL");
static struct dentry *debugfs_test_root;
static struct dentry *debugfs_file;
static ssize_t f_read(struct file *filp, char __user *buf,
size_t len, loff_t *off)
{
int ret;
char val[20] = "read";
/* fs/libfs.c */
ret = simple_read_from_buff(buf, len, off, val, strlen(val));
return ret;
}
struct file_operations debugfs_fops = {
.read = f_read,
};
/**
* 这里只是简单的test,只为了解流程
*/
static int __init create_debugfs_file_init(void)
{
/* 创建根文件夹 */
debugfs_test_root = debugfs_create_dir("debugfs_test", NULL);
/* 创建文件file */
debugfs_file = debugfs_create_file("debugfs_file", 0777, debugfs_test_root,
NULL, &debugfs_fops);
return 0;
}
/**
* debugfs_remove_recursive可以帮我们逐步移除每个分配的dentry,
* 如果您想一个一个手动的移除,也可以直接调用debugfs_remove。
*/
static void __exit create_debugfs_file_exit(void)
{
debugfs_remove(debugfs_file); /* 倒序释放 */
debugfs_remove(debugfs_test_root);
}
module_init(create_debugfs_file_init);
module_exit(create_debugfs_file_exit);
MODULE_AUTHOR("gavinlu1990@gmail.com");
MODULE_DESCRIPTION("create debugfs file");