首页 > 代码库 > 简单的debugfs模型

简单的debugfs模型

#include <linux/init.h>
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm-generic/uaccess.h>


static char abc_str[32] =
{ };
static char blob_inof[32] = "dragon blob wrapper\n";
static struct debugfs_blob_wrapper blob;
static u32 my_u32;
static u32 my_x32;
static u16 my_x16;
static u16 my_u16;


static struct dentry *dragon_root;
static struct dentry *dragon_sub;


static int abc_open(struct inode * inode, struct file * file)
{
file->private_data = http://www.mamicode.com/inode->i_private;
return 0;
}


static ssize_t abc_read(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_read size=%ld\n", size);
if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_to_user(buf, abc_str + *offset, size))
{
return -EFAULT;
}


*offset += size;
return size;
}


static ssize_t abc_write(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_write size=%ld\n", size);


if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_from_user(abc_str + *offset, buf, size))
{
return -EFAULT;
}


*offset += size;
return size;
}
struct file_operations abc =
{ .owner = THIS_MODULE, .open = abc_open, .read = abc_read, .write =
abc_write, };






static int hello_init(void)
{
printk("###dragon### %s\n", __FUNCTION__);


//在根目录中创建dragon_debug文件夹
dragon_root = debugfs_create_dir("dragon_debug", NULL);
if (IS_ERR(dragon_root))
{
printk("DEBUGFS DIR create failed\n");
return -1;
}


dragon_sub = debugfs_create_dir("sub", dragon_root);
if (IS_ERR(dragon_sub))
{
debugfs_remove_recursive(dragon_root);
printk("DEBUGFS DIR create failed\n");
return -1;
}


//注册任意访问文件
struct dentry * entry = debugfs_create_file("abc", 0644, dragon_root, NULL,
&abc);
if (IS_ERR(entry))
{
debugfs_remove_recursive(dragon_root);
dragon_root = NULL;
printk("###dragon### abc create error\n");
return -1;
}




//注册只读字符串
blob.data = http://www.mamicode.com/blob_inof;
blob.size = strlen(blob_inof);
debugfs_create_blob("blob", 0644, dragon_root, &blob);


//注册直接访问读写的整形
debugfs_create_u32("u32", 0644, dragon_root, &my_u32);
debugfs_create_u32("x32", 0644, dragon_root, &my_x32);
debugfs_create_u32("x16", 0644, dragon_root, &my_x16);
debugfs_create_u32("u16", 0644, dragon_root, &my_u16);
return 0;
}


static void hello_exit(void)
{
printk("###dragon### %s\r\n", __FUNCTION__);
debugfs_remove_recursive(dragon_root);
}


MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);

简单的debugfs模型