读书人

debugfs实例 可以当做模板使用

发布时间: 2013-03-17 13:48:31 作者: rapoo

debugfs范例 可以当做模板使用

#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/wait.h>

#include <mach/debug_mm.h>
#include <mach/msm_smd.h>


#define MAX_LEN 64
#ifdef CONFIG_DEBUG_FS
static struct dentry *test_dentry;
#endif
static char l_buf[MAX_LEN];
static unsigned int test_enable;

static ssize_t test_debug_open(struct inode *inode, struct file *file)
{
file->private_data = inode->i_private;
printk("test debugfs opened\n");
return 0;
}

static ssize_t test_debug_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
int len;

if (count < 0)
return 0;
len = count > (MAX_LEN - 1) ? (MAX_LEN - 1) : count;
if (copy_from_user(l_buf, buf, len)) {
printk("Unable to copy data from user space\n");
return -EFAULT;
}
l_buf[len] = 0;
if (l_buf[len - 1] == '\n') {
l_buf[len - 1] = 0;
len--;
}
if (!strncmp(l_buf, "boom", MAX_LEN)) {
printk("boom called -------------\n");
} else if (!strncmp(l_buf, "enable", MAX_LEN)) {
test_enable = 1;
printk("test enabled ------------- : %d\n", test_enable);
} else if (!strncmp(l_buf, "disable", MAX_LEN)) {
test_enable = 0;
printk("test disabled ---------------: %d\n", test_enable);
} else
printk("Unknown Command\n");

return count;
}

static const struct file_operations test_debug_fops = {
.write = test_debug_write,
.open = test_debug_open,
};

static int __init test_debug_init(void)
{
#ifdef CONFIG_DEBUG_FS
test_dentry = debugfs_create_file("test_debug", S_IFREG | S_IRUGO,
NULL, (void *) NULL, &test_debug_fops);
#endif /* CONFIG_DEBUG_FS */
return 0;
}
device_initcall(test_debug_init);

build;

adb shell
mount -t debugfs none /mnt
或者:
#mkdir /data/debug
#mount -t debugfs debugfs /data/debug
#cd /data/debug

echo boom > /mnt/test_debug
dmesg:
<4>[ 881.573233] test debugfs opened
<4>[ 881.576895] boom called -------------

echo disable > /mnt/test_debug
dmesg:
<4>[ 1224.983946] test debugfs opened
<4>[ 1224.986296] test disabled ---------------: 0

echo enable > /mnt/test_debug
dmesg:
<4>[ 926.626735] test debugfs opened
<4>[ 926.632046] test enabled ------------- : 1

读书人网 >移动开发

热点推荐