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