linux创建debugfs文件并进行读写操作

#############

基于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");


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值