debugfs范例 可以当做模板使用

26 篇文章 1 订阅
24 篇文章 0 订阅

#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

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值