Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | .. include:: <isonum.txt> |
| 3 | |
| 4 | ======= |
| 5 | DebugFS |
| 6 | ======= |
| 7 | |
| 8 | Copyright |copy| 2009 Jonathan Corbet <corbet@lwn.net> |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 9 | |
| 10 | Debugfs exists as a simple way for kernel developers to make information |
| 11 | available to user space. Unlike /proc, which is only meant for information |
| 12 | about a process, or sysfs, which has strict one-value-per-file rules, |
| 13 | debugfs has no rules at all. Developers can put any information they want |
| 14 | there. The debugfs filesystem is also intended to not serve as a stable |
| 15 | ABI to user space; in theory, there are no stability constraints placed on |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 16 | files exported there. The real world is not always so simple, though [1]_; |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 17 | even debugfs interfaces are best designed with the idea that they will need |
| 18 | to be maintained forever. |
| 19 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 20 | Debugfs is typically mounted with a command like:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 21 | |
| 22 | mount -t debugfs none /sys/kernel/debug |
| 23 | |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 24 | (Or an equivalent /etc/fstab line). |
Kees Cook | 82aceae4 | 2012-08-27 13:32:15 -0700 | [diff] [blame] | 25 | The debugfs root directory is accessible only to the root user by |
| 26 | default. To change access to the tree the "uid", "gid" and "mode" mount |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 27 | options can be used. |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 28 | |
| 29 | Note that the debugfs API is exported GPL-only to modules. |
| 30 | |
| 31 | Code using debugfs should include <linux/debugfs.h>. Then, the first order |
| 32 | of business will be to create at least one directory to hold a set of |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 33 | debugfs files:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 34 | |
| 35 | struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); |
| 36 | |
| 37 | This call, if successful, will make a directory called name underneath the |
| 38 | indicated parent directory. If parent is NULL, the directory will be |
| 39 | created in the debugfs root. On success, the return value is a struct |
| 40 | dentry pointer which can be used to create files in the directory (and to |
Ronald Tschalär | 9abb249 | 2019-04-15 01:25:05 -0700 | [diff] [blame] | 41 | clean it up at the end). An ERR_PTR(-ERROR) return value indicates that |
| 42 | something went wrong. If ERR_PTR(-ENODEV) is returned, that is an |
| 43 | indication that the kernel has been built without debugfs support and none |
| 44 | of the functions described below will work. |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 45 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 46 | The most general way to create a file within a debugfs directory is with:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 47 | |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 48 | struct dentry *debugfs_create_file(const char *name, umode_t mode, |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 49 | struct dentry *parent, void *data, |
| 50 | const struct file_operations *fops); |
| 51 | |
| 52 | Here, name is the name of the file to create, mode describes the access |
| 53 | permissions the file should have, parent indicates the directory which |
| 54 | should hold the file, data will be stored in the i_private field of the |
| 55 | resulting inode structure, and fops is a set of file operations which |
| 56 | implement the file's behavior. At a minimum, the read() and/or write() |
| 57 | operations should be provided; others can be included as needed. Again, |
Ronald Tschalär | 9abb249 | 2019-04-15 01:25:05 -0700 | [diff] [blame] | 58 | the return value will be a dentry pointer to the created file, |
| 59 | ERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is |
| 60 | missing. |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 61 | |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 62 | Create a file with an initial size, the following function can be used |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 63 | instead:: |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 64 | |
Greg Kroah-Hartman | 526ee72 | 2020-03-09 17:36:40 +0100 | [diff] [blame] | 65 | void debugfs_create_file_size(const char *name, umode_t mode, |
| 66 | struct dentry *parent, void *data, |
| 67 | const struct file_operations *fops, |
| 68 | loff_t file_size); |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 69 | |
| 70 | file_size is the initial file size. The other parameters are the same |
| 71 | as the function debugfs_create_file. |
| 72 | |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 73 | In a number of cases, the creation of a set of file operations is not |
| 74 | actually necessary; the debugfs code provides a number of helper functions |
| 75 | for simple situations. Files containing a single integer value can be |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 76 | created with any of:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 77 | |
Greg Kroah-Hartman | 9655ac4 | 2019-10-11 15:29:24 +0200 | [diff] [blame] | 78 | void debugfs_create_u8(const char *name, umode_t mode, |
| 79 | struct dentry *parent, u8 *value); |
Greg Kroah-Hartman | 313f5db | 2019-10-11 15:29:25 +0200 | [diff] [blame] | 80 | void debugfs_create_u16(const char *name, umode_t mode, |
| 81 | struct dentry *parent, u16 *value); |
Greg Kroah-Hartman | 2b07021 | 2020-04-16 16:54:48 +0200 | [diff] [blame] | 82 | void debugfs_create_u32(const char *name, umode_t mode, |
| 83 | struct dentry *parent, u32 *value); |
Greg Kroah-Hartman | ad26221 | 2019-10-11 15:29:26 +0200 | [diff] [blame] | 84 | void debugfs_create_u64(const char *name, umode_t mode, |
| 85 | struct dentry *parent, u64 *value); |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 86 | |
| 87 | These files support both reading and writing the given value; if a specific |
| 88 | file should not be written to, simply set the mode bits accordingly. The |
| 89 | values in these files are in decimal; if hexadecimal is more appropriate, |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 90 | the following functions can be used instead:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 91 | |
Greg Kroah-Hartman | c7c1168 | 2019-10-11 15:29:28 +0200 | [diff] [blame] | 92 | void debugfs_create_x8(const char *name, umode_t mode, |
| 93 | struct dentry *parent, u8 *value); |
Greg Kroah-Hartman | e40d38f | 2019-10-11 15:29:29 +0200 | [diff] [blame] | 94 | void debugfs_create_x16(const char *name, umode_t mode, |
| 95 | struct dentry *parent, u16 *value); |
Greg Kroah-Hartman | f5cb0a7 | 2019-10-11 15:29:30 +0200 | [diff] [blame] | 96 | void debugfs_create_x32(const char *name, umode_t mode, |
| 97 | struct dentry *parent, u32 *value); |
Greg Kroah-Hartman | 0864c40 | 2019-10-11 15:29:31 +0200 | [diff] [blame] | 98 | void debugfs_create_x64(const char *name, umode_t mode, |
| 99 | struct dentry *parent, u64 *value); |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 100 | |
| 101 | These functions are useful as long as the developer knows the size of the |
| 102 | value to be exported. Some types can have different widths on different |
Geert Uytterhoeven | 726ce47 | 2019-10-21 17:06:45 +0200 | [diff] [blame] | 103 | architectures, though, complicating the situation somewhat. There are |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 104 | functions meant to help out in such special cases:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 105 | |
Greg Kroah-Hartman | 8e58026 | 2019-10-11 15:29:27 +0200 | [diff] [blame] | 106 | void debugfs_create_size_t(const char *name, umode_t mode, |
| 107 | struct dentry *parent, size_t *value); |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 108 | |
| 109 | As might be expected, this function will create a debugfs file to represent |
| 110 | a variable of type size_t. |
| 111 | |
Geert Uytterhoeven | d350475 | 2019-10-25 11:41:24 +0200 | [diff] [blame] | 112 | Similarly, there are helpers for variables of type unsigned long, in decimal |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 113 | and hexadecimal:: |
Geert Uytterhoeven | 726ce47 | 2019-10-21 17:06:45 +0200 | [diff] [blame] | 114 | |
| 115 | struct dentry *debugfs_create_ulong(const char *name, umode_t mode, |
| 116 | struct dentry *parent, |
| 117 | unsigned long *value); |
Geert Uytterhoeven | d350475 | 2019-10-25 11:41:24 +0200 | [diff] [blame] | 118 | void debugfs_create_xul(const char *name, umode_t mode, |
| 119 | struct dentry *parent, unsigned long *value); |
Geert Uytterhoeven | 726ce47 | 2019-10-21 17:06:45 +0200 | [diff] [blame] | 120 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 121 | Boolean values can be placed in debugfs with:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 122 | |
Greg Kroah-Hartman | 393b063 | 2021-05-21 20:45:19 +0200 | [diff] [blame] | 123 | void debugfs_create_bool(const char *name, umode_t mode, |
| 124 | struct dentry *parent, bool *value); |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 125 | |
| 126 | A read on the resulting file will yield either Y (for non-zero values) or |
| 127 | N, followed by a newline. If written to, it will accept either upper- or |
| 128 | lower-case values, or 1 or 0. Any other input will be silently ignored. |
| 129 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 130 | Also, atomic_t values can be placed in debugfs with:: |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 131 | |
Greg Kroah-Hartman | 9927c6f | 2019-10-16 06:03:32 -0700 | [diff] [blame] | 132 | void debugfs_create_atomic_t(const char *name, umode_t mode, |
| 133 | struct dentry *parent, atomic_t *value) |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 134 | |
| 135 | A read of this file will get atomic_t values, and a write of this file |
| 136 | will set atomic_t values. |
| 137 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 138 | Another option is exporting a block of arbitrary binary data, with |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 139 | this structure and function:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 140 | |
| 141 | struct debugfs_blob_wrapper { |
| 142 | void *data; |
| 143 | unsigned long size; |
| 144 | }; |
| 145 | |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 146 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 147 | struct dentry *parent, |
| 148 | struct debugfs_blob_wrapper *blob); |
| 149 | |
| 150 | A read of this file will return the data pointed to by the |
| 151 | debugfs_blob_wrapper structure. Some drivers use "blobs" as a simple way |
| 152 | to return several lines of (static) formatted text output. This function |
| 153 | can be used to export binary information, but there does not appear to be |
| 154 | any code which does so in the mainline. Note that all files created with |
| 155 | debugfs_create_blob() are read-only. |
| 156 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 157 | If you want to dump a block of registers (something that happens quite |
| 158 | often during development, even if little such code reaches mainline. |
| 159 | Debugfs offers two functions: one to make a registers-only file, and |
| 160 | another to insert a register block in the middle of another sequential |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 161 | file:: |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 162 | |
| 163 | struct debugfs_reg32 { |
| 164 | char *name; |
| 165 | unsigned long offset; |
| 166 | }; |
| 167 | |
| 168 | struct debugfs_regset32 { |
Rikard Falkeborn | fd79cfd | 2020-05-08 23:29:49 +0200 | [diff] [blame] | 169 | const struct debugfs_reg32 *regs; |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 170 | int nregs; |
| 171 | void __iomem *base; |
Rikard Falkeborn | fd79cfd | 2020-05-08 23:29:49 +0200 | [diff] [blame] | 172 | struct device *dev; /* Optional device for Runtime PM */ |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
Greg Kroah-Hartman | ae91c92 | 2019-11-22 11:44:53 +0100 | [diff] [blame] | 175 | debugfs_create_regset32(const char *name, umode_t mode, |
| 176 | struct dentry *parent, |
| 177 | struct debugfs_regset32 *regset); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 178 | |
Rikard Falkeborn | fd79cfd | 2020-05-08 23:29:49 +0200 | [diff] [blame] | 179 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 180 | int nregs, void __iomem *base, char *prefix); |
| 181 | |
| 182 | The "base" argument may be 0, but you may want to build the reg32 array |
| 183 | using __stringify, and a number of register names (macros) are actually |
| 184 | byte offsets over a base for the register block. |
| 185 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 186 | If you want to dump an u32 array in debugfs, you can create file with:: |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 187 | |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 188 | struct debugfs_u32_array { |
| 189 | u32 *array; |
| 190 | u32 n_elements; |
| 191 | }; |
| 192 | |
Greg Kroah-Hartman | c9c2c27 | 2019-04-16 15:46:55 +0200 | [diff] [blame] | 193 | void debugfs_create_u32_array(const char *name, umode_t mode, |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 194 | struct dentry *parent, |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 195 | struct debugfs_u32_array *array); |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 196 | |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 197 | The "array" argument wraps a pointer to the array's data and the number |
| 198 | of its elements. Note: Once array is created its size can not be changed. |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 199 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 200 | There is a helper function to create device related seq_file:: |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 201 | |
Greg Kroah-Hartman | 0d519cb | 2020-10-23 15:10:37 +0200 | [diff] [blame] | 202 | void debugfs_create_devm_seqfile(struct device *dev, |
Wang Long | 9e1aa7c | 2015-07-16 06:31:16 +0000 | [diff] [blame] | 203 | const char *name, |
| 204 | struct dentry *parent, |
| 205 | int (*read_fn)(struct seq_file *s, |
| 206 | void *data)); |
| 207 | |
| 208 | The "dev" argument is the device related to this debugfs file, and |
| 209 | the "read_fn" is a function pointer which to be called to print the |
| 210 | seq_file content. |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 211 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 212 | There are a couple of other directory-oriented helper functions:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 213 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 214 | struct dentry *debugfs_rename(struct dentry *old_dir, |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 215 | struct dentry *old_dentry, |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 216 | struct dentry *new_dir, |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 217 | const char *new_name); |
| 218 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 219 | struct dentry *debugfs_create_symlink(const char *name, |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 220 | struct dentry *parent, |
| 221 | const char *target); |
| 222 | |
| 223 | A call to debugfs_rename() will give a new name to an existing debugfs |
| 224 | file, possibly in a different directory. The new_name must not exist prior |
| 225 | to the call; the return value is old_dentry with updated information. |
| 226 | Symbolic links can be created with debugfs_create_symlink(). |
| 227 | |
| 228 | There is one important thing that all debugfs users must take into account: |
| 229 | there is no automatic cleanup of any directories created in debugfs. If a |
| 230 | module is unloaded without explicitly removing debugfs entries, the result |
| 231 | will be a lot of stale pointers and no end of highly antisocial behavior. |
| 232 | So all debugfs users - at least those which can be built as modules - must |
| 233 | be prepared to remove all files and directories they create there. A file |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 234 | can be removed with:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 235 | |
| 236 | void debugfs_remove(struct dentry *dentry); |
| 237 | |
Ronald Tschalär | 9abb249 | 2019-04-15 01:25:05 -0700 | [diff] [blame] | 238 | The dentry value can be NULL or an error value, in which case nothing will |
| 239 | be removed. |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 240 | |
| 241 | Once upon a time, debugfs users were required to remember the dentry |
| 242 | pointer for every debugfs file they created so that all files could be |
| 243 | cleaned up. We live in more civilized times now, though, and debugfs users |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 244 | can call:: |
Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 245 | |
| 246 | void debugfs_remove_recursive(struct dentry *dentry); |
| 247 | |
| 248 | If this function is passed a pointer for the dentry corresponding to the |
| 249 | top-level directory, the entire hierarchy below that directory will be |
| 250 | removed. |
| 251 | |
Mauro Carvalho Chehab | 5744378 | 2020-02-17 17:11:57 +0100 | [diff] [blame] | 252 | .. [1] http://lwn.net/Articles/309298/ |