-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugfs-demo.c
105 lines (81 loc) · 2.08 KB
/
debugfs-demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <asm/uaccess.h>
#define MAX_LEN 100
struct dentry *demo_debugfs_root;
u8 p0 = 100;
char str[MAX_LEN] = "Hello DebugFs!\n";
struct debugfs_blob_wrapper p1;
static int p2_open(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}
static ssize_t p2_read(struct file *filp, char __user *buffer,
size_t count, loff_t *ppos)
{
if (*ppos >= MAX_LEN)
return 0;
if (*ppos + count > MAX_LEN)
count = MAX_LEN - *ppos;
// memset(buffer, 0, MAX_LEN);
if (copy_to_user(buffer, str + *ppos, count))
return -EFAULT;
*ppos += count;
return count;
}
static ssize_t p2_write(struct file *filp, const char __user *buffer,
size_t count, loff_t *ppos)
{
if (*ppos >= MAX_LEN)
return 0;
if (*ppos + count > MAX_LEN)
count = MAX_LEN - *ppos;
// memset(str, 0, MAX_LEN);
if (copy_from_user(str + *ppos, buffer, count))
return -EFAULT;
*ppos += count;
return count;
}
struct file_operations p2_fops = {
.owner = THIS_MODULE,
.open = p2_open,
.read = p2_read,
.write = p2_write,
};
static int __init demo_debugfs_init(void)
{
struct dentry *sub_dir, *p0_dentry, *p1_dentry, *p2_dentry;
demo_debugfs_root = debugfs_create_dir("debugfs-demo", NULL);
if (!demo_debugfs_root)
return -ENOENT;
p0_dentry = debugfs_create_u8("p0", 0644, demo_debugfs_root, &p0);
if (!p0_dentry)
goto Fail;
p1.data = (void *)str;
p1.size = strlen(str) + 1;
p1_dentry = debugfs_create_blob("p1", 0644, demo_debugfs_root, &p1);
if (!p1_dentry)
goto Fail;
sub_dir = debugfs_create_dir("subdir", demo_debugfs_root);
if (!sub_dir)
goto Fail;
p2_dentry = debugfs_create_file("p2", 0644, sub_dir, NULL, &p2_fops);
if (!p2_dentry)
goto Fail;
return 0;
Fail:
debugfs_remove_recursive(demo_debugfs_root);
demo_debugfs_root = NULL;
return -ENOENT;
}
static void __exit demo_debugfs_exit(void)
{
debugfs_remove_recursive(demo_debugfs_root);
return;
}
module_init(demo_debugfs_init);
module_exit(demo_debugfs_exit);
MODULE_LICENSE("GPL");