Simple debugfs Model

Source: Internet
Author: User
# 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 2010my_x16;
Static 2010my_2010;


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 __);


// Create the dragon_debug folder in the root directory
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;
}


// Register any Access File
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;
}




// Register a read-only string
Blob. Data = blob_inof;
Blob. size = strlen (blob_inof );
Debugfs_create_blob ("blob", 0644, dragon_root, & BLOB );


// Register an integer for direct access to read/write
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 ("2010", 0644, dragon_root, & my_2010 );
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 );

Simple debugfs Model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.