sysfs
在 sysfs 上添加一个文件节点,用于导出一些 debug 信息。
| 12
 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
 
 | static ssize_t reset_store(struct device *dev, struct device_attribute *devatte, const char *buf, size_t size)
 {
 int ret;
 bool enable;
 
 ret = kstrtobool(buf, &enable);
 if (ret)
 return ret;
 
 if (enable) {
 dev_info(dev, "reset\n");
 }
 
 return strlen(buf);
 }
 static DEVICE_ATTR_WO(reset);
 
 static ssize_t debug_show(struct device *dev, struct device_attribute *devatte, char *buf)
 {
 
 sprintf(buf, "This is a debug node\n");
 
 sprintf(buf, "%sAdd debug info\n", buf);
 
 return strlen(buf);
 }
 static DEVICE_ATTR_RO(debug);
 
 static struct attribute *test_attr[] = {
 &dev_attr_debug.attr,
 &dev_attr_reset.attr,
 NULL
 };
 
 static int stsfs_create(struct device *dev)
 {
 int i;
 
 for (i = 0; i < ARRAY_SIZE(test_attr); i++) {
 if (!test_attr[i])
 break;
 
 if (sysfs_add_file_to_group(&dev->kobj, test_attr[i], NULL) < 0) {
 dev_err(dev, "Failed to craeate test attr.\n");
 return -1;
 }
 }
 return 0;
 }
 
 
 | 
debugfs
在 debugfs 系统上创建一个节点,调试时需要先挂载 debugfs。
实现过程可参考 ab8500-debugfs.c
| 1
 | mount -t debugfs none mnt/
 | 
过程:
- 在 debugfs 上创建一个 dir
- 在 dir 下创建文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | static int ge_debug_show(struct seq_file *s, void *p)
 {
 seq_printf(s, "This is a debug node\n");
 
 seq_printf(s, "%sAdd debug info\n");
 return 0;
 }
 DEFINE_SHOW_ATTRIBUTE(ge_debug)
 
 static int xxx_probe(struct platform_device *pdev)
 {
 struct dentry *debug_dir;
 
 debug_dir = debugfs_create_dir("test", NULL);
 
 debugfs_create_file("file_name", (S_IRUGO | S_IWUSR | S_IWGRP), debug_dir, &pdev->dev, &ge_debug_fops);
 
 }
 
 
 | 
DEFINE_SHOW_ATTRIBUTE会展开,简化代码。详情参考 include/linux/seq_file.h
Linux 5.10 未对 reset 节点的实现进行封装。参考 ab8500-debugfs.c实现,代码较繁琐。