02_debugfsLinux内核模块

01_basicLinux内核模块-CSDN博客环境ID=ubuntuMakefilemodules:clean:basic.creturn 0;运行效果。https://blog.csdn.net/m0_37132481/article/details/136157384?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22136157384%22%2C%22source%22%3A%22m0_37132481%22%7Ddebugfs.c

root@T:/media/sf_D_DRIVE/kmodule/02_debugfs# cat debugfs.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/debugfs.h>

#define TAG "HELLO# "

static struct dentry *debugfs_dir_entry_root = NULL;
struct my_data {
        char buf[10];
};
static struct my_data g_data = {0};
static int my_open(struct inode *i, struct file *file)
{
        printk(TAG "%s called\n", __func__);
        return 0;
}

static ssize_t my_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
        struct my_data *data = &g_data;

        return simple_read_from_buffer(buf, size, ppos, data->buf, sizeof(data->buf));
}
static ssize_t my_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
{
        struct my_data *data = &g_data;

        /* in case of echo command */
        if(size > sizeof(buf))
        {
                return -EINVAL;
        }
        else
        {
                /* nothing to do */
        }
        return simple_write_to_buffer(data->buf, sizeof(data->buf), ppos, buf, size);
}
static const struct file_operations data_fops = {
        .owner          = THIS_MODULE,
        .open           = my_open,
        .read           = my_read,
        .write          = my_write,
};

static int debugfs_init(void)
{
        printk(TAG "%s called\n", __func__);
        /*
         * create /sys/kernel/debug/hello/data
        */
        debugfs_dir_entry_root = debugfs_create_dir("hello", NULL);
        debugfs_create_file("data", 0777, debugfs_dir_entry_root, NULL, &data_fops);
        return 0;
}
static void debugfs_exit(void)
{
        printk(TAG "%s called\n", __func__);
        debugfs_remove_recursive(debugfs_dir_entry_root);
}

module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");

效果

root@T:/media/sf_D_DRIVE/kmodule/02_debugfs# ls /sys/kernel/debug/hello/data
/sys/kernel/debug/hello/data
 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值