现在的位置: 首页 > 综合 > 正文

debugfs范例 可以当做模板使用

2013年10月14日 ⁄ 综合 ⁄ 共 2939字 ⁄ 字号 评论关闭

#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 1024
#ifdef CONFIG_DEBUG_FS
static struct dentry *test_dentry;
#endif
char l_buf[MAX_LEN];
static  int test_enable;
static unsigned int test_enable1=1;
static unsigned int test_enable2=2;
static unsigned int test_enable3=3;

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
{
   sscanf(l_buf,"woshiwojia%d",&test_enable);
   printk("test_enable = %d\n",test_enable);
}

return count;
}

static ssize_t test_debug_read(struct file *file,  char __user *buf,
size_t count, loff_t *ppos)
{
    int len = 0;
int tot = 0;
int dlen;
char *bp;
    

if (*ppos)
return 0;
/* the end */

bp = l_buf;
dlen = sizeof(l_buf);

len = snprintf(bp, dlen, "\nmdp:\n");
bp += len;
dlen -= len;

len = snprintf(bp, dlen, "test_enable0 %d\t",
test_enable);
bp += len;
dlen -= len;

len = snprintf(bp, dlen, "test_enable1 %d\t",
test_enable1);
bp += len;
dlen -= len;

    len = snprintf(bp, dlen, "test_enable2 %d\n",
test_enable2);
bp += len;
dlen -= len;

printk("bp = %d\n",(unsigned int)bp);
    printk("bp+1 = %d\n",(unsigned int)++bp);
    
len = snprintf(bp, dlen, "test_enable3 %d\n",
test_enable3);
bp += len;
dlen -= len;
   

tot = (int)bp - (int)l_buf;
*bp = 0;
tot++;
 
    
if (tot < 0)
return 0;
if (copy_to_user(buf, l_buf, tot))
return -EFAULT;

*ppos += tot;
/* increase offset */

return tot;

}

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

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

 

 

 

抱歉!评论已关闭.