利用debugfs来调试模块

http://www.cnblogs.com/wwang/archive/2011/01/17/1937609.html


kernel中编译进debugfs

Kernel hacking
    [*] Debug filesystem


CONFIG_DEBUG_FS


挂载debugfs

默认情况会挂载在/sys/kernel/debug下,如果没有挂载可以下面的命令挂载。

mount -t debugfs none /dir/you/want


下面是个例子~


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <asm/uaccess.h>

struct dentry *my_debugfs_root;

u8 a = 0;
char hello[32] = "Hello world!\n";
struct debugfs_blob_wrapper b;

static int c_open(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}

static ssize_t c_read(struct file *filp, char __user *buffer,
size_t count, loff_t *ppos)
{
if (*ppos >= 32)
return 0;
if (*ppos + count > 32)
count = 32 - *ppos;

if (copy_to_user(buffer, hello + *ppos, count))
return -EFAULT;

*ppos += count;

return count;
}

static ssize_t c_write(struct file *filp, const char __user *buffer,
size_t count, loff_t *ppos)
{
if (*ppos >= 32)
return 0;
if (*ppos + count > 32)
count = 32 - *ppos;

if (copy_from_user(hello + *ppos, buffer, count))
return -EFAULT;

*ppos += count;

return count;
}

struct file_operations c_fops = {
.owner = THIS_MODULE,
.open = c_open,
.read = c_read,
.write = c_write,
};

static int __init mydebugfs_init(void)
{
struct dentry *sub_dir, *r_a, *r_b, *s_c;

printk(KERN_INFO "mydebugfs_init\n");

my_debugfs_root = debugfs_create_dir("mydebug", NULL);
if (!my_debugfs_root)
return -ENOENT;

r_a = debugfs_create_u8("a", 0644, my_debugfs_root, &a);
if (!r_a)
goto Fail;

/* debugfs_blob_wrapper is read only */
b.data = (void *)hello;
b.size = strlen(hello) + 1;
r_b = debugfs_create_blob("b", 0644, my_debugfs_root, &b);
if (!r_b)
goto Fail;

sub_dir = debugfs_create_dir("subdir", my_debugfs_root);
if (!sub_dir)
goto Fail;

/* debufs_create_file could be read/write */
s_c = debugfs_create_file("c", 0644, sub_dir, NULL, &c_fops);
if (!s_c)
goto Fail;

return 0;

Fail:
debugfs_remove_recursive(my_debugfs_root);
my_debugfs_root = NULL;
return -ENOENT;
}

static void __exit mydebugfs_exit(void)
{
printk(KERN_INFO "mydebugfs_exit\n");

debugfs_remove_recursive(my_debugfs_root);

return;
}

module_init(mydebugfs_init);
module_exit(mydebugfs_exit);

MODULE_LICENSE("GPL");


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值