18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci.. include:: ../disclaimer-zh_CN.rst
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci:Original: :doc:`../../../filesystems/debugfs`
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci=======
88c2ecf20Sopenharmony_ciDebugfs
98c2ecf20Sopenharmony_ci=======
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci译者
128c2ecf20Sopenharmony_ci::
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci	中文版维护者: 罗楚成 Chucheng Luo <luochucheng@vivo.com>
158c2ecf20Sopenharmony_ci	中文版翻译者: 罗楚成 Chucheng Luo <luochucheng@vivo.com>
168c2ecf20Sopenharmony_ci	中文版校译者:  罗楚成 Chucheng Luo <luochucheng@vivo.com>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci版权所有2020 罗楚成 <luochucheng@vivo.com>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciDebugfs是内核开发人员在用户空间获取信息的简单方法。与/proc不同,proc只提供进程
248c2ecf20Sopenharmony_ci信息。也不像sysfs,具有严格的“每个文件一个值“的规则。debugfs根本没有规则,开发
258c2ecf20Sopenharmony_ci人员可以在这里放置他们想要的任何信息。debugfs文件系统也不能用作稳定的ABI接口。
268c2ecf20Sopenharmony_ci从理论上讲,debugfs导出文件的时候没有任何约束。但是[1]实际情况并不总是那么
278c2ecf20Sopenharmony_ci简单。即使是debugfs接口,也最好根据需要进行设计,并尽量保持接口不变。
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciDebugfs通常使用以下命令安装::
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci    mount -t debugfs none /sys/kernel/debug
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci(或等效的/etc/fstab行)。
358c2ecf20Sopenharmony_cidebugfs根目录默认仅可由root用户访问。要更改对文件树的访问,请使用“ uid”,“ gid”
368c2ecf20Sopenharmony_ci和“ mode”挂载选项。请注意,debugfs API仅按照GPL协议导出到模块。
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci使用debugfs的代码应包含<linux/debugfs.h>。然后,首先是创建至少一个目录来保存
398c2ecf20Sopenharmony_ci一组debugfs文件::
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci如果成功,此调用将在指定的父目录下创建一个名为name的目录。如果parent参数为空,
448c2ecf20Sopenharmony_ci则会在debugfs根目录中创建。创建目录成功时,返回值是一个指向dentry结构体的指针。
458c2ecf20Sopenharmony_ci该dentry结构体的指针可用于在目录中创建文件(以及最后将其清理干净)。ERR_PTR
468c2ecf20Sopenharmony_ci(-ERROR)返回值表明出错。如果返回ERR_PTR(-ENODEV),则表明内核是在没有debugfs
478c2ecf20Sopenharmony_ci支持的情况下构建的,并且下述函数都不会起作用。
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci在debugfs目录中创建文件的最通用方法是::
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_file(const char *name, umode_t mode,
528c2ecf20Sopenharmony_ci				       struct dentry *parent, void *data,
538c2ecf20Sopenharmony_ci				       const struct file_operations *fops);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci在这里,name是要创建的文件的名称,mode描述了访问文件应具有的权限,parent指向
568c2ecf20Sopenharmony_ci应该保存文件的目录,data将存储在产生的inode结构体的i_private字段中,而fops是
578c2ecf20Sopenharmony_ci一组文件操作函数,这些函数中实现文件操作的具体行为。至少,read()和/或
588c2ecf20Sopenharmony_ciwrite()操作应提供;其他可以根据需要包括在内。同样的,返回值将是指向创建文件
598c2ecf20Sopenharmony_ci的dentry指针,错误时返回ERR_PTR(-ERROR),系统不支持debugfs时返回值为ERR_PTR
608c2ecf20Sopenharmony_ci(-ENODEV)。创建一个初始大小的文件,可以使用以下函数代替::
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
638c2ecf20Sopenharmony_ci				struct dentry *parent, void *data,
648c2ecf20Sopenharmony_ci				const struct file_operations *fops,
658c2ecf20Sopenharmony_ci				loff_t file_size);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cifile_size是初始文件大小。其他参数跟函数debugfs_create_file的相同。
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci在许多情况下,没必要自己去创建一组文件操作;对于一些简单的情况,debugfs代码提供
708c2ecf20Sopenharmony_ci了许多帮助函数。包含单个整数值的文件可以使用以下任何一项创建::
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci    void debugfs_create_u8(const char *name, umode_t mode,
738c2ecf20Sopenharmony_ci			   struct dentry *parent, u8 *value);
748c2ecf20Sopenharmony_ci    void debugfs_create_u16(const char *name, umode_t mode,
758c2ecf20Sopenharmony_ci			    struct dentry *parent, u16 *value);
768c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_u32(const char *name, umode_t mode,
778c2ecf20Sopenharmony_ci				      struct dentry *parent, u32 *value);
788c2ecf20Sopenharmony_ci    void debugfs_create_u64(const char *name, umode_t mode,
798c2ecf20Sopenharmony_ci			    struct dentry *parent, u64 *value);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci这些文件支持读取和写入给定值。如果某个文件不支持写入,只需根据需要设置mode
828c2ecf20Sopenharmony_ci参数位。这些文件中的值以十进制表示;如果需要使用十六进制,可以使用以下函数
838c2ecf20Sopenharmony_ci替代::
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci    void debugfs_create_x8(const char *name, umode_t mode,
868c2ecf20Sopenharmony_ci			   struct dentry *parent, u8 *value);
878c2ecf20Sopenharmony_ci    void debugfs_create_x16(const char *name, umode_t mode,
888c2ecf20Sopenharmony_ci			    struct dentry *parent, u16 *value);
898c2ecf20Sopenharmony_ci    void debugfs_create_x32(const char *name, umode_t mode,
908c2ecf20Sopenharmony_ci			    struct dentry *parent, u32 *value);
918c2ecf20Sopenharmony_ci    void debugfs_create_x64(const char *name, umode_t mode,
928c2ecf20Sopenharmony_ci			    struct dentry *parent, u64 *value);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci这些功能只有在开发人员知道导出值的大小的时候才有用。某些数据类型在不同的架构上
958c2ecf20Sopenharmony_ci有不同的宽度,这样会使情况变得有些复杂。在这种特殊情况下可以使用以下函数::
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci    void debugfs_create_size_t(const char *name, umode_t mode,
988c2ecf20Sopenharmony_ci			       struct dentry *parent, size_t *value);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci不出所料,此函数将创建一个debugfs文件来表示类型为size_t的变量。
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci同样地,也有导出无符号长整型变量的函数,分别以十进制和十六进制表示如下::
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
1058c2ecf20Sopenharmony_ci					struct dentry *parent,
1068c2ecf20Sopenharmony_ci					unsigned long *value);
1078c2ecf20Sopenharmony_ci    void debugfs_create_xul(const char *name, umode_t mode,
1088c2ecf20Sopenharmony_ci			    struct dentry *parent, unsigned long *value);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci布尔值可以通过以下方式放置在debugfs中::
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_bool(const char *name, umode_t mode,
1138c2ecf20Sopenharmony_ci				       struct dentry *parent, bool *value);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci读取结果文件将产生Y(对于非零值)或N,后跟换行符写入的时候,它只接受大写或小写
1178c2ecf20Sopenharmony_ci值或1或0。任何其他输入将被忽略。
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci同样,atomic_t类型的值也可以放置在debugfs中::
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci    void debugfs_create_atomic_t(const char *name, umode_t mode,
1228c2ecf20Sopenharmony_ci				 struct dentry *parent, atomic_t *value)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci读取此文件将获得atomic_t值,写入此文件将设置atomic_t值。
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci另一个选择是通过以下结构体和函数导出一个任意二进制数据块::
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci    struct debugfs_blob_wrapper {
1298c2ecf20Sopenharmony_ci	void *data;
1308c2ecf20Sopenharmony_ci	unsigned long size;
1318c2ecf20Sopenharmony_ci    };
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1348c2ecf20Sopenharmony_ci				       struct dentry *parent,
1358c2ecf20Sopenharmony_ci				       struct debugfs_blob_wrapper *blob);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci读取此文件将返回由指针指向debugfs_blob_wrapper结构体的数据。一些驱动使用“blobs”
1388c2ecf20Sopenharmony_ci作为一种返回几行(静态)格式化文本的简单方法。这个函数可用于导出二进制信息,但
1398c2ecf20Sopenharmony_ci似乎在主线中没有任何代码这样做。请注意,使用debugfs_create_blob()命令创建的
1408c2ecf20Sopenharmony_ci所有文件是只读的。
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci如果您要转储一个寄存器块(在开发过程中经常会这么做,但是这样的调试代码很少上传
1438c2ecf20Sopenharmony_ci到主线中。Debugfs提供两个函数:一个用于创建仅寄存器文件,另一个把一个寄存器块
1448c2ecf20Sopenharmony_ci插入一个顺序文件中::
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci    struct debugfs_reg32 {
1478c2ecf20Sopenharmony_ci	char *name;
1488c2ecf20Sopenharmony_ci	unsigned long offset;
1498c2ecf20Sopenharmony_ci    };
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci    struct debugfs_regset32 {
1528c2ecf20Sopenharmony_ci	struct debugfs_reg32 *regs;
1538c2ecf20Sopenharmony_ci	int nregs;
1548c2ecf20Sopenharmony_ci	void __iomem *base;
1558c2ecf20Sopenharmony_ci    };
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
1588c2ecf20Sopenharmony_ci				     struct dentry *parent,
1598c2ecf20Sopenharmony_ci				     struct debugfs_regset32 *regset);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci    void debugfs_print_regs32(struct seq_file *s, struct debugfs_reg32 *regs,
1628c2ecf20Sopenharmony_ci			 int nregs, void __iomem *base, char *prefix);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci“base”参数可能为0,但您可能需要使用__stringify构建reg32数组,实际上有许多寄存器
1658c2ecf20Sopenharmony_ci名称(宏)是寄存器块在基址上的字节偏移量。
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci如果要在debugfs中转储u32数组,可以使用以下函数创建文件::
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci     void debugfs_create_u32_array(const char *name, umode_t mode,
1708c2ecf20Sopenharmony_ci			struct dentry *parent,
1718c2ecf20Sopenharmony_ci			u32 *array, u32 elements);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci“array”参数提供数据,而“elements”参数为数组中元素的数量。注意:数组创建后,数组
1748c2ecf20Sopenharmony_ci大小无法更改。
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci有一个函数来创建与设备相关的seq_file::
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci   struct dentry *debugfs_create_devm_seqfile(struct device *dev,
1798c2ecf20Sopenharmony_ci				const char *name,
1808c2ecf20Sopenharmony_ci				struct dentry *parent,
1818c2ecf20Sopenharmony_ci				int (*read_fn)(struct seq_file *s,
1828c2ecf20Sopenharmony_ci					void *data));
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci“dev”参数是与此debugfs文件相关的设备,并且“read_fn”是一个函数指针,这个函数在
1858c2ecf20Sopenharmony_ci打印seq_file内容的时候被回调。
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci还有一些其他的面向目录的函数::
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci    struct dentry *debugfs_rename(struct dentry *old_dir,
1908c2ecf20Sopenharmony_ci		                  struct dentry *old_dentry,
1918c2ecf20Sopenharmony_ci		                  struct dentry *new_dir,
1928c2ecf20Sopenharmony_ci				  const char *new_name);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_symlink(const char *name,
1958c2ecf20Sopenharmony_ci                                          struct dentry *parent,
1968c2ecf20Sopenharmony_ci                                          const char *target);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci调用debugfs_rename()将为现有的debugfs文件重命名,可能同时切换目录。 new_name
1998c2ecf20Sopenharmony_ci函数调用之前不能存在;返回值为old_dentry,其中包含更新的信息。可以使用
2008c2ecf20Sopenharmony_cidebugfs_create_symlink()创建符号链接。
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci所有debugfs用户必须考虑的一件事是:
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cidebugfs不会自动清除在其中创建的任何目录。如果一个模块在不显式删除debugfs目录的
2058c2ecf20Sopenharmony_ci情况下卸载模块,结果将会遗留很多野指针,从而导致系统不稳定。因此,所有debugfs
2068c2ecf20Sopenharmony_ci用户-至少是那些可以作为模块构建的用户-必须做模块卸载的时候准备删除在此创建的
2078c2ecf20Sopenharmony_ci所有文件和目录。一份文件可以通过以下方式删除::
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci    void debugfs_remove(struct dentry *dentry);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cidentry值可以为NULL或错误值,在这种情况下,不会有任何文件被删除。
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci很久以前,内核开发者使用debugfs时需要记录他们创建的每个dentry指针,以便最后所有
2148c2ecf20Sopenharmony_ci文件都可以被清理掉。但是,现在debugfs用户能调用以下函数递归清除之前创建的文件::
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci    void debugfs_remove_recursive(struct dentry *dentry);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci如果将对应顶层目录的dentry传递给以上函数,则该目录下的整个层次结构将会被删除。
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci注释:
2218c2ecf20Sopenharmony_ci[1] http://lwn.net/Articles/309298/
222