簡單的debugfs模型

來源:互聯網
上載者:User

標籤:style   io   ar   檔案   cti   sp   on   amp   c   

#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 = 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 = 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模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.