利用debugfs來調試模組

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.