xref: /linux/Documentation/filesystems/debugfs.rst (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
157443789SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
257443789SMauro Carvalho Chehab.. include:: <isonum.txt>
357443789SMauro Carvalho Chehab
457443789SMauro Carvalho Chehab=======
557443789SMauro Carvalho ChehabDebugFS
657443789SMauro Carvalho Chehab=======
757443789SMauro Carvalho Chehab
857443789SMauro Carvalho ChehabCopyright |copy| 2009 Jonathan Corbet <corbet@lwn.net>
957443789SMauro Carvalho Chehab
1057443789SMauro Carvalho ChehabDebugfs exists as a simple way for kernel developers to make information
1157443789SMauro Carvalho Chehabavailable to user space.  Unlike /proc, which is only meant for information
1257443789SMauro Carvalho Chehababout a process, or sysfs, which has strict one-value-per-file rules,
1357443789SMauro Carvalho Chehabdebugfs has no rules at all.  Developers can put any information they want
1457443789SMauro Carvalho Chehabthere.  The debugfs filesystem is also intended to not serve as a stable
1557443789SMauro Carvalho ChehabABI to user space; in theory, there are no stability constraints placed on
1657443789SMauro Carvalho Chehabfiles exported there.  The real world is not always so simple, though [1]_;
1757443789SMauro Carvalho Chehabeven debugfs interfaces are best designed with the idea that they will need
1857443789SMauro Carvalho Chehabto be maintained forever.
1957443789SMauro Carvalho Chehab
2057443789SMauro Carvalho ChehabDebugfs is typically mounted with a command like::
2157443789SMauro Carvalho Chehab
2257443789SMauro Carvalho Chehab    mount -t debugfs none /sys/kernel/debug
2357443789SMauro Carvalho Chehab
2457443789SMauro Carvalho Chehab(Or an equivalent /etc/fstab line).
2557443789SMauro Carvalho ChehabThe debugfs root directory is accessible only to the root user by
2657443789SMauro Carvalho Chehabdefault. To change access to the tree the "uid", "gid" and "mode" mount
2757443789SMauro Carvalho Chehaboptions can be used.
2857443789SMauro Carvalho Chehab
2957443789SMauro Carvalho ChehabNote that the debugfs API is exported GPL-only to modules.
3057443789SMauro Carvalho Chehab
3157443789SMauro Carvalho ChehabCode using debugfs should include <linux/debugfs.h>.  Then, the first order
3257443789SMauro Carvalho Chehabof business will be to create at least one directory to hold a set of
3357443789SMauro Carvalho Chehabdebugfs files::
3457443789SMauro Carvalho Chehab
3557443789SMauro Carvalho Chehab    struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
3657443789SMauro Carvalho Chehab
3757443789SMauro Carvalho ChehabThis call, if successful, will make a directory called name underneath the
3857443789SMauro Carvalho Chehabindicated parent directory.  If parent is NULL, the directory will be
3957443789SMauro Carvalho Chehabcreated in the debugfs root.  On success, the return value is a struct
4057443789SMauro Carvalho Chehabdentry pointer which can be used to create files in the directory (and to
4157443789SMauro Carvalho Chehabclean it up at the end).  An ERR_PTR(-ERROR) return value indicates that
4257443789SMauro Carvalho Chehabsomething went wrong.  If ERR_PTR(-ENODEV) is returned, that is an
4357443789SMauro Carvalho Chehabindication that the kernel has been built without debugfs support and none
4457443789SMauro Carvalho Chehabof the functions described below will work.
4557443789SMauro Carvalho Chehab
4657443789SMauro Carvalho ChehabThe most general way to create a file within a debugfs directory is with::
4757443789SMauro Carvalho Chehab
4857443789SMauro Carvalho Chehab    struct dentry *debugfs_create_file(const char *name, umode_t mode,
4957443789SMauro Carvalho Chehab				       struct dentry *parent, void *data,
5057443789SMauro Carvalho Chehab				       const struct file_operations *fops);
5157443789SMauro Carvalho Chehab
5257443789SMauro Carvalho ChehabHere, name is the name of the file to create, mode describes the access
5357443789SMauro Carvalho Chehabpermissions the file should have, parent indicates the directory which
5457443789SMauro Carvalho Chehabshould hold the file, data will be stored in the i_private field of the
5557443789SMauro Carvalho Chehabresulting inode structure, and fops is a set of file operations which
5657443789SMauro Carvalho Chehabimplement the file's behavior.  At a minimum, the read() and/or write()
5757443789SMauro Carvalho Chehaboperations should be provided; others can be included as needed.  Again,
5857443789SMauro Carvalho Chehabthe return value will be a dentry pointer to the created file,
5957443789SMauro Carvalho ChehabERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is
6057443789SMauro Carvalho Chehabmissing.
6157443789SMauro Carvalho Chehab
6257443789SMauro Carvalho ChehabCreate a file with an initial size, the following function can be used
6357443789SMauro Carvalho Chehabinstead::
6457443789SMauro Carvalho Chehab
6559838093SLinus Torvalds    void debugfs_create_file_size(const char *name, umode_t mode,
6657443789SMauro Carvalho Chehab				  struct dentry *parent, void *data,
6757443789SMauro Carvalho Chehab				  const struct file_operations *fops,
6857443789SMauro Carvalho Chehab				  loff_t file_size);
6957443789SMauro Carvalho Chehab
7057443789SMauro Carvalho Chehabfile_size is the initial file size. The other parameters are the same
7157443789SMauro Carvalho Chehabas the function debugfs_create_file.
7257443789SMauro Carvalho Chehab
7357443789SMauro Carvalho ChehabIn a number of cases, the creation of a set of file operations is not
7457443789SMauro Carvalho Chehabactually necessary; the debugfs code provides a number of helper functions
7557443789SMauro Carvalho Chehabfor simple situations.  Files containing a single integer value can be
7657443789SMauro Carvalho Chehabcreated with any of::
7757443789SMauro Carvalho Chehab
7857443789SMauro Carvalho Chehab    void debugfs_create_u8(const char *name, umode_t mode,
7957443789SMauro Carvalho Chehab			   struct dentry *parent, u8 *value);
8057443789SMauro Carvalho Chehab    void debugfs_create_u16(const char *name, umode_t mode,
8157443789SMauro Carvalho Chehab			    struct dentry *parent, u16 *value);
822b07021aSGreg Kroah-Hartman    void debugfs_create_u32(const char *name, umode_t mode,
8357443789SMauro Carvalho Chehab			    struct dentry *parent, u32 *value);
8457443789SMauro Carvalho Chehab    void debugfs_create_u64(const char *name, umode_t mode,
8557443789SMauro Carvalho Chehab			    struct dentry *parent, u64 *value);
8657443789SMauro Carvalho Chehab
8757443789SMauro Carvalho ChehabThese files support both reading and writing the given value; if a specific
8857443789SMauro Carvalho Chehabfile should not be written to, simply set the mode bits accordingly.  The
8957443789SMauro Carvalho Chehabvalues in these files are in decimal; if hexadecimal is more appropriate,
9057443789SMauro Carvalho Chehabthe following functions can be used instead::
9157443789SMauro Carvalho Chehab
9257443789SMauro Carvalho Chehab    void debugfs_create_x8(const char *name, umode_t mode,
9357443789SMauro Carvalho Chehab			   struct dentry *parent, u8 *value);
9457443789SMauro Carvalho Chehab    void debugfs_create_x16(const char *name, umode_t mode,
9557443789SMauro Carvalho Chehab			    struct dentry *parent, u16 *value);
9657443789SMauro Carvalho Chehab    void debugfs_create_x32(const char *name, umode_t mode,
9757443789SMauro Carvalho Chehab			    struct dentry *parent, u32 *value);
9857443789SMauro Carvalho Chehab    void debugfs_create_x64(const char *name, umode_t mode,
9957443789SMauro Carvalho Chehab			    struct dentry *parent, u64 *value);
10057443789SMauro Carvalho Chehab
10157443789SMauro Carvalho ChehabThese functions are useful as long as the developer knows the size of the
10257443789SMauro Carvalho Chehabvalue to be exported.  Some types can have different widths on different
10357443789SMauro Carvalho Chehabarchitectures, though, complicating the situation somewhat.  There are
10457443789SMauro Carvalho Chehabfunctions meant to help out in such special cases::
10557443789SMauro Carvalho Chehab
10657443789SMauro Carvalho Chehab    void debugfs_create_size_t(const char *name, umode_t mode,
10757443789SMauro Carvalho Chehab			       struct dentry *parent, size_t *value);
10857443789SMauro Carvalho Chehab
10957443789SMauro Carvalho ChehabAs might be expected, this function will create a debugfs file to represent
11057443789SMauro Carvalho Chehaba variable of type size_t.
11157443789SMauro Carvalho Chehab
11257443789SMauro Carvalho ChehabSimilarly, there are helpers for variables of type unsigned long, in decimal
11357443789SMauro Carvalho Chehaband hexadecimal::
11457443789SMauro Carvalho Chehab
11557443789SMauro Carvalho Chehab    struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
11657443789SMauro Carvalho Chehab					struct dentry *parent,
11757443789SMauro Carvalho Chehab					unsigned long *value);
11857443789SMauro Carvalho Chehab    void debugfs_create_xul(const char *name, umode_t mode,
11957443789SMauro Carvalho Chehab			    struct dentry *parent, unsigned long *value);
12057443789SMauro Carvalho Chehab
12157443789SMauro Carvalho ChehabBoolean values can be placed in debugfs with::
12257443789SMauro Carvalho Chehab
123393b0638SGreg Kroah-Hartman    void debugfs_create_bool(const char *name, umode_t mode,
12457443789SMauro Carvalho Chehab                             struct dentry *parent, bool *value);
12557443789SMauro Carvalho Chehab
12657443789SMauro Carvalho ChehabA read on the resulting file will yield either Y (for non-zero values) or
12757443789SMauro Carvalho ChehabN, followed by a newline.  If written to, it will accept either upper- or
12857443789SMauro Carvalho Chehablower-case values, or 1 or 0.  Any other input will be silently ignored.
12957443789SMauro Carvalho Chehab
13057443789SMauro Carvalho ChehabAlso, atomic_t values can be placed in debugfs with::
13157443789SMauro Carvalho Chehab
13257443789SMauro Carvalho Chehab    void debugfs_create_atomic_t(const char *name, umode_t mode,
13357443789SMauro Carvalho Chehab				 struct dentry *parent, atomic_t *value)
13457443789SMauro Carvalho Chehab
13557443789SMauro Carvalho ChehabA read of this file will get atomic_t values, and a write of this file
13657443789SMauro Carvalho Chehabwill set atomic_t values.
13757443789SMauro Carvalho Chehab
13857443789SMauro Carvalho ChehabAnother option is exporting a block of arbitrary binary data, with
13957443789SMauro Carvalho Chehabthis structure and function::
14057443789SMauro Carvalho Chehab
14157443789SMauro Carvalho Chehab    struct debugfs_blob_wrapper {
14257443789SMauro Carvalho Chehab	void *data;
14357443789SMauro Carvalho Chehab	unsigned long size;
14457443789SMauro Carvalho Chehab    };
14557443789SMauro Carvalho Chehab
14657443789SMauro Carvalho Chehab    struct dentry *debugfs_create_blob(const char *name, umode_t mode,
14757443789SMauro Carvalho Chehab				       struct dentry *parent,
14857443789SMauro Carvalho Chehab				       struct debugfs_blob_wrapper *blob);
14957443789SMauro Carvalho Chehab
15057443789SMauro Carvalho ChehabA read of this file will return the data pointed to by the
15157443789SMauro Carvalho Chehabdebugfs_blob_wrapper structure.  Some drivers use "blobs" as a simple way
15257443789SMauro Carvalho Chehabto return several lines of (static) formatted text output.  This function
15357443789SMauro Carvalho Chehabcan be used to export binary information, but there does not appear to be
15457443789SMauro Carvalho Chehabany code which does so in the mainline.  Note that all files created with
15557443789SMauro Carvalho Chehabdebugfs_create_blob() are read-only.
15657443789SMauro Carvalho Chehab
15757443789SMauro Carvalho ChehabIf you want to dump a block of registers (something that happens quite
158*5cd4cd0aSRandy Dunlapoften during development, even if little such code reaches mainline),
159*5cd4cd0aSRandy Dunlapdebugfs offers two functions: one to make a registers-only file, and
16057443789SMauro Carvalho Chehabanother to insert a register block in the middle of another sequential
16157443789SMauro Carvalho Chehabfile::
16257443789SMauro Carvalho Chehab
16357443789SMauro Carvalho Chehab    struct debugfs_reg32 {
16457443789SMauro Carvalho Chehab	char *name;
16557443789SMauro Carvalho Chehab	unsigned long offset;
16657443789SMauro Carvalho Chehab    };
16757443789SMauro Carvalho Chehab
16857443789SMauro Carvalho Chehab    struct debugfs_regset32 {
169fd79cfd7SRikard Falkeborn	const struct debugfs_reg32 *regs;
17057443789SMauro Carvalho Chehab	int nregs;
17157443789SMauro Carvalho Chehab	void __iomem *base;
172fd79cfd7SRikard Falkeborn	struct device *dev;     /* Optional device for Runtime PM */
17357443789SMauro Carvalho Chehab    };
17457443789SMauro Carvalho Chehab
175481ed297SLinus Torvalds    debugfs_create_regset32(const char *name, umode_t mode,
17657443789SMauro Carvalho Chehab			    struct dentry *parent,
17757443789SMauro Carvalho Chehab			    struct debugfs_regset32 *regset);
17857443789SMauro Carvalho Chehab
179fd79cfd7SRikard Falkeborn    void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
18057443789SMauro Carvalho Chehab			 int nregs, void __iomem *base, char *prefix);
18157443789SMauro Carvalho Chehab
18257443789SMauro Carvalho ChehabThe "base" argument may be 0, but you may want to build the reg32 array
18357443789SMauro Carvalho Chehabusing __stringify, and a number of register names (macros) are actually
18457443789SMauro Carvalho Chehabbyte offsets over a base for the register block.
18557443789SMauro Carvalho Chehab
186*5cd4cd0aSRandy DunlapIf you want to dump a u32 array in debugfs, you can create a file with::
18757443789SMauro Carvalho Chehab
188a2b992c8SJakub Kicinski    struct debugfs_u32_array {
189a2b992c8SJakub Kicinski	u32 *array;
190a2b992c8SJakub Kicinski	u32 n_elements;
191a2b992c8SJakub Kicinski    };
192a2b992c8SJakub Kicinski
19357443789SMauro Carvalho Chehab    void debugfs_create_u32_array(const char *name, umode_t mode,
19457443789SMauro Carvalho Chehab			struct dentry *parent,
195a2b992c8SJakub Kicinski			struct debugfs_u32_array *array);
19657443789SMauro Carvalho Chehab
197a2b992c8SJakub KicinskiThe "array" argument wraps a pointer to the array's data and the number
198a2b992c8SJakub Kicinskiof its elements. Note: Once array is created its size can not be changed.
19957443789SMauro Carvalho Chehab
200*5cd4cd0aSRandy DunlapThere is a helper function to create a device-related seq_file::
20157443789SMauro Carvalho Chehab
2020d519cbfSGreg Kroah-Hartman   void debugfs_create_devm_seqfile(struct device *dev,
20357443789SMauro Carvalho Chehab				const char *name,
20457443789SMauro Carvalho Chehab				struct dentry *parent,
20557443789SMauro Carvalho Chehab				int (*read_fn)(struct seq_file *s,
20657443789SMauro Carvalho Chehab					void *data));
20757443789SMauro Carvalho Chehab
20857443789SMauro Carvalho ChehabThe "dev" argument is the device related to this debugfs file, and
20957443789SMauro Carvalho Chehabthe "read_fn" is a function pointer which to be called to print the
21057443789SMauro Carvalho Chehabseq_file content.
21157443789SMauro Carvalho Chehab
21257443789SMauro Carvalho ChehabThere are a couple of other directory-oriented helper functions::
21357443789SMauro Carvalho Chehab
21457443789SMauro Carvalho Chehab    struct dentry *debugfs_rename(struct dentry *old_dir,
21557443789SMauro Carvalho Chehab    				  struct dentry *old_dentry,
21657443789SMauro Carvalho Chehab		                  struct dentry *new_dir,
21757443789SMauro Carvalho Chehab				  const char *new_name);
21857443789SMauro Carvalho Chehab
21957443789SMauro Carvalho Chehab    struct dentry *debugfs_create_symlink(const char *name,
22057443789SMauro Carvalho Chehab                                          struct dentry *parent,
22157443789SMauro Carvalho Chehab				      	  const char *target);
22257443789SMauro Carvalho Chehab
22357443789SMauro Carvalho ChehabA call to debugfs_rename() will give a new name to an existing debugfs
22457443789SMauro Carvalho Chehabfile, possibly in a different directory.  The new_name must not exist prior
22557443789SMauro Carvalho Chehabto the call; the return value is old_dentry with updated information.
22657443789SMauro Carvalho ChehabSymbolic links can be created with debugfs_create_symlink().
22757443789SMauro Carvalho Chehab
22857443789SMauro Carvalho ChehabThere is one important thing that all debugfs users must take into account:
22957443789SMauro Carvalho Chehabthere is no automatic cleanup of any directories created in debugfs.  If a
23057443789SMauro Carvalho Chehabmodule is unloaded without explicitly removing debugfs entries, the result
23157443789SMauro Carvalho Chehabwill be a lot of stale pointers and no end of highly antisocial behavior.
23257443789SMauro Carvalho ChehabSo all debugfs users - at least those which can be built as modules - must
23357443789SMauro Carvalho Chehabbe prepared to remove all files and directories they create there.  A file
23457443789SMauro Carvalho Chehabcan be removed with::
23557443789SMauro Carvalho Chehab
23657443789SMauro Carvalho Chehab    void debugfs_remove(struct dentry *dentry);
23757443789SMauro Carvalho Chehab
23857443789SMauro Carvalho ChehabThe dentry value can be NULL or an error value, in which case nothing will
23957443789SMauro Carvalho Chehabbe removed.
24057443789SMauro Carvalho Chehab
24157443789SMauro Carvalho ChehabOnce upon a time, debugfs users were required to remember the dentry
24257443789SMauro Carvalho Chehabpointer for every debugfs file they created so that all files could be
24357443789SMauro Carvalho Chehabcleaned up.  We live in more civilized times now, though, and debugfs users
24457443789SMauro Carvalho Chehabcan call::
24557443789SMauro Carvalho Chehab
24657443789SMauro Carvalho Chehab    void debugfs_remove_recursive(struct dentry *dentry);
24757443789SMauro Carvalho Chehab
24857443789SMauro Carvalho ChehabIf this function is passed a pointer for the dentry corresponding to the
24957443789SMauro Carvalho Chehabtop-level directory, the entire hierarchy below that directory will be
25057443789SMauro Carvalho Chehabremoved.
25157443789SMauro Carvalho Chehab
25257443789SMauro Carvalho Chehab.. [1] http://lwn.net/Articles/309298/
253