18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci.. include:: <isonum.txt>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci=======
58c2ecf20Sopenharmony_ciDebugFS
68c2ecf20Sopenharmony_ci=======
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciCopyright |copy| 2009 Jonathan Corbet <corbet@lwn.net>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciDebugfs exists as a simple way for kernel developers to make information
118c2ecf20Sopenharmony_ciavailable to user space.  Unlike /proc, which is only meant for information
128c2ecf20Sopenharmony_ciabout a process, or sysfs, which has strict one-value-per-file rules,
138c2ecf20Sopenharmony_cidebugfs has no rules at all.  Developers can put any information they want
148c2ecf20Sopenharmony_cithere.  The debugfs filesystem is also intended to not serve as a stable
158c2ecf20Sopenharmony_ciABI to user space; in theory, there are no stability constraints placed on
168c2ecf20Sopenharmony_cifiles exported there.  The real world is not always so simple, though [1]_;
178c2ecf20Sopenharmony_cieven debugfs interfaces are best designed with the idea that they will need
188c2ecf20Sopenharmony_cito be maintained forever.
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciDebugfs is typically mounted with a command like::
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci    mount -t debugfs none /sys/kernel/debug
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci(Or an equivalent /etc/fstab line).
258c2ecf20Sopenharmony_ciThe debugfs root directory is accessible only to the root user by
268c2ecf20Sopenharmony_cidefault. To change access to the tree the "uid", "gid" and "mode" mount
278c2ecf20Sopenharmony_cioptions can be used.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciNote that the debugfs API is exported GPL-only to modules.
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciCode using debugfs should include <linux/debugfs.h>.  Then, the first order
328c2ecf20Sopenharmony_ciof business will be to create at least one directory to hold a set of
338c2ecf20Sopenharmony_cidebugfs files::
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciThis call, if successful, will make a directory called name underneath the
388c2ecf20Sopenharmony_ciindicated parent directory.  If parent is NULL, the directory will be
398c2ecf20Sopenharmony_cicreated in the debugfs root.  On success, the return value is a struct
408c2ecf20Sopenharmony_cidentry pointer which can be used to create files in the directory (and to
418c2ecf20Sopenharmony_ciclean it up at the end).  An ERR_PTR(-ERROR) return value indicates that
428c2ecf20Sopenharmony_cisomething went wrong.  If ERR_PTR(-ENODEV) is returned, that is an
438c2ecf20Sopenharmony_ciindication that the kernel has been built without debugfs support and none
448c2ecf20Sopenharmony_ciof the functions described below will work.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciThe most general way to create a file within a debugfs directory is with::
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_file(const char *name, umode_t mode,
498c2ecf20Sopenharmony_ci				       struct dentry *parent, void *data,
508c2ecf20Sopenharmony_ci				       const struct file_operations *fops);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciHere, name is the name of the file to create, mode describes the access
538c2ecf20Sopenharmony_cipermissions the file should have, parent indicates the directory which
548c2ecf20Sopenharmony_cishould hold the file, data will be stored in the i_private field of the
558c2ecf20Sopenharmony_ciresulting inode structure, and fops is a set of file operations which
568c2ecf20Sopenharmony_ciimplement the file's behavior.  At a minimum, the read() and/or write()
578c2ecf20Sopenharmony_cioperations should be provided; others can be included as needed.  Again,
588c2ecf20Sopenharmony_cithe return value will be a dentry pointer to the created file,
598c2ecf20Sopenharmony_ciERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is
608c2ecf20Sopenharmony_cimissing.
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciCreate a file with an initial size, the following function can be used
638c2ecf20Sopenharmony_ciinstead::
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci    void debugfs_create_file_size(const char *name, umode_t mode,
668c2ecf20Sopenharmony_ci				  struct dentry *parent, void *data,
678c2ecf20Sopenharmony_ci				  const struct file_operations *fops,
688c2ecf20Sopenharmony_ci				  loff_t file_size);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cifile_size is the initial file size. The other parameters are the same
718c2ecf20Sopenharmony_cias the function debugfs_create_file.
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciIn a number of cases, the creation of a set of file operations is not
748c2ecf20Sopenharmony_ciactually necessary; the debugfs code provides a number of helper functions
758c2ecf20Sopenharmony_cifor simple situations.  Files containing a single integer value can be
768c2ecf20Sopenharmony_cicreated with any of::
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci    void debugfs_create_u8(const char *name, umode_t mode,
798c2ecf20Sopenharmony_ci			   struct dentry *parent, u8 *value);
808c2ecf20Sopenharmony_ci    void debugfs_create_u16(const char *name, umode_t mode,
818c2ecf20Sopenharmony_ci			    struct dentry *parent, u16 *value);
828c2ecf20Sopenharmony_ci    void debugfs_create_u32(const char *name, umode_t mode,
838c2ecf20Sopenharmony_ci			    struct dentry *parent, u32 *value);
848c2ecf20Sopenharmony_ci    void debugfs_create_u64(const char *name, umode_t mode,
858c2ecf20Sopenharmony_ci			    struct dentry *parent, u64 *value);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciThese files support both reading and writing the given value; if a specific
888c2ecf20Sopenharmony_cifile should not be written to, simply set the mode bits accordingly.  The
898c2ecf20Sopenharmony_civalues in these files are in decimal; if hexadecimal is more appropriate,
908c2ecf20Sopenharmony_cithe following functions can be used instead::
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci    void debugfs_create_x8(const char *name, umode_t mode,
938c2ecf20Sopenharmony_ci			   struct dentry *parent, u8 *value);
948c2ecf20Sopenharmony_ci    void debugfs_create_x16(const char *name, umode_t mode,
958c2ecf20Sopenharmony_ci			    struct dentry *parent, u16 *value);
968c2ecf20Sopenharmony_ci    void debugfs_create_x32(const char *name, umode_t mode,
978c2ecf20Sopenharmony_ci			    struct dentry *parent, u32 *value);
988c2ecf20Sopenharmony_ci    void debugfs_create_x64(const char *name, umode_t mode,
998c2ecf20Sopenharmony_ci			    struct dentry *parent, u64 *value);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciThese functions are useful as long as the developer knows the size of the
1028c2ecf20Sopenharmony_civalue to be exported.  Some types can have different widths on different
1038c2ecf20Sopenharmony_ciarchitectures, though, complicating the situation somewhat.  There are
1048c2ecf20Sopenharmony_cifunctions meant to help out in such special cases::
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci    void debugfs_create_size_t(const char *name, umode_t mode,
1078c2ecf20Sopenharmony_ci			       struct dentry *parent, size_t *value);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciAs might be expected, this function will create a debugfs file to represent
1108c2ecf20Sopenharmony_cia variable of type size_t.
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciSimilarly, there are helpers for variables of type unsigned long, in decimal
1138c2ecf20Sopenharmony_ciand hexadecimal::
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
1168c2ecf20Sopenharmony_ci					struct dentry *parent,
1178c2ecf20Sopenharmony_ci					unsigned long *value);
1188c2ecf20Sopenharmony_ci    void debugfs_create_xul(const char *name, umode_t mode,
1198c2ecf20Sopenharmony_ci			    struct dentry *parent, unsigned long *value);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciBoolean values can be placed in debugfs with::
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_bool(const char *name, umode_t mode,
1248c2ecf20Sopenharmony_ci				       struct dentry *parent, bool *value);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciA read on the resulting file will yield either Y (for non-zero values) or
1278c2ecf20Sopenharmony_ciN, followed by a newline.  If written to, it will accept either upper- or
1288c2ecf20Sopenharmony_cilower-case values, or 1 or 0.  Any other input will be silently ignored.
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciAlso, atomic_t values can be placed in debugfs with::
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci    void debugfs_create_atomic_t(const char *name, umode_t mode,
1338c2ecf20Sopenharmony_ci				 struct dentry *parent, atomic_t *value)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciA read of this file will get atomic_t values, and a write of this file
1368c2ecf20Sopenharmony_ciwill set atomic_t values.
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciAnother option is exporting a block of arbitrary binary data, with
1398c2ecf20Sopenharmony_cithis structure and function::
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci    struct debugfs_blob_wrapper {
1428c2ecf20Sopenharmony_ci	void *data;
1438c2ecf20Sopenharmony_ci	unsigned long size;
1448c2ecf20Sopenharmony_ci    };
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1478c2ecf20Sopenharmony_ci				       struct dentry *parent,
1488c2ecf20Sopenharmony_ci				       struct debugfs_blob_wrapper *blob);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciA read of this file will return the data pointed to by the
1518c2ecf20Sopenharmony_cidebugfs_blob_wrapper structure.  Some drivers use "blobs" as a simple way
1528c2ecf20Sopenharmony_cito return several lines of (static) formatted text output.  This function
1538c2ecf20Sopenharmony_cican be used to export binary information, but there does not appear to be
1548c2ecf20Sopenharmony_ciany code which does so in the mainline.  Note that all files created with
1558c2ecf20Sopenharmony_cidebugfs_create_blob() are read-only.
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciIf you want to dump a block of registers (something that happens quite
1588c2ecf20Sopenharmony_cioften during development, even if little such code reaches mainline.
1598c2ecf20Sopenharmony_ciDebugfs offers two functions: one to make a registers-only file, and
1608c2ecf20Sopenharmony_cianother to insert a register block in the middle of another sequential
1618c2ecf20Sopenharmony_cifile::
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci    struct debugfs_reg32 {
1648c2ecf20Sopenharmony_ci	char *name;
1658c2ecf20Sopenharmony_ci	unsigned long offset;
1668c2ecf20Sopenharmony_ci    };
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci    struct debugfs_regset32 {
1698c2ecf20Sopenharmony_ci	const struct debugfs_reg32 *regs;
1708c2ecf20Sopenharmony_ci	int nregs;
1718c2ecf20Sopenharmony_ci	void __iomem *base;
1728c2ecf20Sopenharmony_ci	struct device *dev;     /* Optional device for Runtime PM */
1738c2ecf20Sopenharmony_ci    };
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci    debugfs_create_regset32(const char *name, umode_t mode,
1768c2ecf20Sopenharmony_ci			    struct dentry *parent,
1778c2ecf20Sopenharmony_ci			    struct debugfs_regset32 *regset);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci    void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1808c2ecf20Sopenharmony_ci			 int nregs, void __iomem *base, char *prefix);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciThe "base" argument may be 0, but you may want to build the reg32 array
1838c2ecf20Sopenharmony_ciusing __stringify, and a number of register names (macros) are actually
1848c2ecf20Sopenharmony_cibyte offsets over a base for the register block.
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciIf you want to dump an u32 array in debugfs, you can create file with::
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci    struct debugfs_u32_array {
1898c2ecf20Sopenharmony_ci	u32 *array;
1908c2ecf20Sopenharmony_ci	u32 n_elements;
1918c2ecf20Sopenharmony_ci    };
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci    void debugfs_create_u32_array(const char *name, umode_t mode,
1948c2ecf20Sopenharmony_ci			struct dentry *parent,
1958c2ecf20Sopenharmony_ci			struct debugfs_u32_array *array);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciThe "array" argument wraps a pointer to the array's data and the number
1988c2ecf20Sopenharmony_ciof its elements. Note: Once array is created its size can not be changed.
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciThere is a helper function to create device related seq_file::
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci   void debugfs_create_devm_seqfile(struct device *dev,
2038c2ecf20Sopenharmony_ci				const char *name,
2048c2ecf20Sopenharmony_ci				struct dentry *parent,
2058c2ecf20Sopenharmony_ci				int (*read_fn)(struct seq_file *s,
2068c2ecf20Sopenharmony_ci					void *data));
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciThe "dev" argument is the device related to this debugfs file, and
2098c2ecf20Sopenharmony_cithe "read_fn" is a function pointer which to be called to print the
2108c2ecf20Sopenharmony_ciseq_file content.
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciThere are a couple of other directory-oriented helper functions::
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci    struct dentry *debugfs_rename(struct dentry *old_dir,
2158c2ecf20Sopenharmony_ci    				  struct dentry *old_dentry,
2168c2ecf20Sopenharmony_ci		                  struct dentry *new_dir,
2178c2ecf20Sopenharmony_ci				  const char *new_name);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci    struct dentry *debugfs_create_symlink(const char *name,
2208c2ecf20Sopenharmony_ci                                          struct dentry *parent,
2218c2ecf20Sopenharmony_ci				      	  const char *target);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciA call to debugfs_rename() will give a new name to an existing debugfs
2248c2ecf20Sopenharmony_cifile, possibly in a different directory.  The new_name must not exist prior
2258c2ecf20Sopenharmony_cito the call; the return value is old_dentry with updated information.
2268c2ecf20Sopenharmony_ciSymbolic links can be created with debugfs_create_symlink().
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciThere is one important thing that all debugfs users must take into account:
2298c2ecf20Sopenharmony_cithere is no automatic cleanup of any directories created in debugfs.  If a
2308c2ecf20Sopenharmony_cimodule is unloaded without explicitly removing debugfs entries, the result
2318c2ecf20Sopenharmony_ciwill be a lot of stale pointers and no end of highly antisocial behavior.
2328c2ecf20Sopenharmony_ciSo all debugfs users - at least those which can be built as modules - must
2338c2ecf20Sopenharmony_cibe prepared to remove all files and directories they create there.  A file
2348c2ecf20Sopenharmony_cican be removed with::
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci    void debugfs_remove(struct dentry *dentry);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ciThe dentry value can be NULL or an error value, in which case nothing will
2398c2ecf20Sopenharmony_cibe removed.
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciOnce upon a time, debugfs users were required to remember the dentry
2428c2ecf20Sopenharmony_cipointer for every debugfs file they created so that all files could be
2438c2ecf20Sopenharmony_cicleaned up.  We live in more civilized times now, though, and debugfs users
2448c2ecf20Sopenharmony_cican call::
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci    void debugfs_remove_recursive(struct dentry *dentry);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciIf this function is passed a pointer for the dentry corresponding to the
2498c2ecf20Sopenharmony_citop-level directory, the entire hierarchy below that directory will be
2508c2ecf20Sopenharmony_ciremoved.
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci.. [1] http://lwn.net/Articles/309298/
253