| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 1 | Copyright 2009 Jonathan Corbet <corbet@lwn.net> | 
|  | 2 |  | 
|  | 3 | Debugfs exists as a simple way for kernel developers to make information | 
|  | 4 | available to user space.  Unlike /proc, which is only meant for information | 
|  | 5 | about a process, or sysfs, which has strict one-value-per-file rules, | 
|  | 6 | debugfs has no rules at all.  Developers can put any information they want | 
|  | 7 | there.  The debugfs filesystem is also intended to not serve as a stable | 
|  | 8 | ABI to user space; in theory, there are no stability constraints placed on | 
|  | 9 | files exported there.  The real world is not always so simple, though [1]; | 
|  | 10 | even debugfs interfaces are best designed with the idea that they will need | 
|  | 11 | to be maintained forever. | 
|  | 12 |  | 
|  | 13 | Debugfs is typically mounted with a command like: | 
|  | 14 |  | 
|  | 15 | mount -t debugfs none /sys/kernel/debug | 
|  | 16 |  | 
|  | 17 | (Or an equivalent /etc/fstab line). | 
|  | 18 |  | 
|  | 19 | Note that the debugfs API is exported GPL-only to modules. | 
|  | 20 |  | 
|  | 21 | Code using debugfs should include <linux/debugfs.h>.  Then, the first order | 
|  | 22 | of business will be to create at least one directory to hold a set of | 
|  | 23 | debugfs files: | 
|  | 24 |  | 
|  | 25 | struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); | 
|  | 26 |  | 
|  | 27 | This call, if successful, will make a directory called name underneath the | 
|  | 28 | indicated parent directory.  If parent is NULL, the directory will be | 
|  | 29 | created in the debugfs root.  On success, the return value is a struct | 
|  | 30 | dentry pointer which can be used to create files in the directory (and to | 
|  | 31 | clean it up at the end).  A NULL return value indicates that something went | 
|  | 32 | wrong.  If ERR_PTR(-ENODEV) is returned, that is an indication that the | 
|  | 33 | kernel has been built without debugfs support and none of the functions | 
|  | 34 | described below will work. | 
|  | 35 |  | 
|  | 36 | The most general way to create a file within a debugfs directory is with: | 
|  | 37 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 38 | struct dentry *debugfs_create_file(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 39 | struct dentry *parent, void *data, | 
|  | 40 | const struct file_operations *fops); | 
|  | 41 |  | 
|  | 42 | Here, name is the name of the file to create, mode describes the access | 
|  | 43 | permissions the file should have, parent indicates the directory which | 
|  | 44 | should hold the file, data will be stored in the i_private field of the | 
|  | 45 | resulting inode structure, and fops is a set of file operations which | 
|  | 46 | implement the file's behavior.  At a minimum, the read() and/or write() | 
|  | 47 | operations should be provided; others can be included as needed.  Again, | 
|  | 48 | the return value will be a dentry pointer to the created file, NULL for | 
|  | 49 | error, or ERR_PTR(-ENODEV) if debugfs support is missing. | 
|  | 50 |  | 
|  | 51 | In a number of cases, the creation of a set of file operations is not | 
|  | 52 | actually necessary; the debugfs code provides a number of helper functions | 
|  | 53 | for simple situations.  Files containing a single integer value can be | 
|  | 54 | created with any of: | 
|  | 55 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 56 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 57 | struct dentry *parent, u8 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 58 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 59 | struct dentry *parent, u16 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 60 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 61 | struct dentry *parent, u32 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 62 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 63 | struct dentry *parent, u64 *value); | 
|  | 64 |  | 
|  | 65 | These files support both reading and writing the given value; if a specific | 
|  | 66 | file should not be written to, simply set the mode bits accordingly.  The | 
|  | 67 | values in these files are in decimal; if hexadecimal is more appropriate, | 
|  | 68 | the following functions can be used instead: | 
|  | 69 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 70 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 71 | struct dentry *parent, u8 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 72 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 73 | struct dentry *parent, u16 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 74 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 75 | struct dentry *parent, u32 *value); | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 76 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, | 
| Akinobu Mita | d0a5426 | 2011-07-09 14:01:17 +0900 | [diff] [blame] | 77 | struct dentry *parent, u64 *value); | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 78 |  | 
|  | 79 | These functions are useful as long as the developer knows the size of the | 
|  | 80 | value to be exported.  Some types can have different widths on different | 
|  | 81 | architectures, though, complicating the situation somewhat.  There is a | 
|  | 82 | function meant to help out in one special case: | 
|  | 83 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 84 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 85 | struct dentry *parent, | 
|  | 86 | size_t *value); | 
|  | 87 |  | 
|  | 88 | As might be expected, this function will create a debugfs file to represent | 
|  | 89 | a variable of type size_t. | 
|  | 90 |  | 
|  | 91 | Boolean values can be placed in debugfs with: | 
|  | 92 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 93 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 94 | struct dentry *parent, u32 *value); | 
|  | 95 |  | 
|  | 96 | A read on the resulting file will yield either Y (for non-zero values) or | 
|  | 97 | N, followed by a newline.  If written to, it will accept either upper- or | 
|  | 98 | lower-case values, or 1 or 0.  Any other input will be silently ignored. | 
|  | 99 |  | 
| Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 100 | Another option is exporting a block of arbitrary binary data, with | 
|  | 101 | this structure and function: | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 102 |  | 
|  | 103 | struct debugfs_blob_wrapper { | 
|  | 104 | void *data; | 
|  | 105 | unsigned long size; | 
|  | 106 | }; | 
|  | 107 |  | 
| Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 108 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 109 | struct dentry *parent, | 
|  | 110 | struct debugfs_blob_wrapper *blob); | 
|  | 111 |  | 
|  | 112 | A read of this file will return the data pointed to by the | 
|  | 113 | debugfs_blob_wrapper structure.  Some drivers use "blobs" as a simple way | 
|  | 114 | to return several lines of (static) formatted text output.  This function | 
|  | 115 | can be used to export binary information, but there does not appear to be | 
|  | 116 | any code which does so in the mainline.  Note that all files created with | 
|  | 117 | debugfs_create_blob() are read-only. | 
|  | 118 |  | 
| Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 119 | If you want to dump a block of registers (something that happens quite | 
|  | 120 | often during development, even if little such code reaches mainline. | 
|  | 121 | Debugfs offers two functions: one to make a registers-only file, and | 
|  | 122 | another to insert a register block in the middle of another sequential | 
|  | 123 | file. | 
|  | 124 |  | 
|  | 125 | struct debugfs_reg32 { | 
|  | 126 | char *name; | 
|  | 127 | unsigned long offset; | 
|  | 128 | }; | 
|  | 129 |  | 
|  | 130 | struct debugfs_regset32 { | 
|  | 131 | struct debugfs_reg32 *regs; | 
|  | 132 | int nregs; | 
|  | 133 | void __iomem *base; | 
|  | 134 | }; | 
|  | 135 |  | 
|  | 136 | struct dentry *debugfs_create_regset32(const char *name, mode_t mode, | 
|  | 137 | struct dentry *parent, | 
|  | 138 | struct debugfs_regset32 *regset); | 
|  | 139 |  | 
|  | 140 | int debugfs_print_regs32(struct seq_file *s, struct debugfs_reg32 *regs, | 
|  | 141 | int nregs, void __iomem *base, char *prefix); | 
|  | 142 |  | 
|  | 143 | The "base" argument may be 0, but you may want to build the reg32 array | 
|  | 144 | using __stringify, and a number of register names (macros) are actually | 
|  | 145 | byte offsets over a base for the register block. | 
|  | 146 |  | 
|  | 147 |  | 
| Jonathan Corbet | f89d7ea | 2009-06-04 16:35:25 -0600 | [diff] [blame] | 148 | There are a couple of other directory-oriented helper functions: | 
|  | 149 |  | 
|  | 150 | struct dentry *debugfs_rename(struct dentry *old_dir, | 
|  | 151 | struct dentry *old_dentry, | 
|  | 152 | struct dentry *new_dir, | 
|  | 153 | const char *new_name); | 
|  | 154 |  | 
|  | 155 | struct dentry *debugfs_create_symlink(const char *name, | 
|  | 156 | struct dentry *parent, | 
|  | 157 | const char *target); | 
|  | 158 |  | 
|  | 159 | A call to debugfs_rename() will give a new name to an existing debugfs | 
|  | 160 | file, possibly in a different directory.  The new_name must not exist prior | 
|  | 161 | to the call; the return value is old_dentry with updated information. | 
|  | 162 | Symbolic links can be created with debugfs_create_symlink(). | 
|  | 163 |  | 
|  | 164 | There is one important thing that all debugfs users must take into account: | 
|  | 165 | there is no automatic cleanup of any directories created in debugfs.  If a | 
|  | 166 | module is unloaded without explicitly removing debugfs entries, the result | 
|  | 167 | will be a lot of stale pointers and no end of highly antisocial behavior. | 
|  | 168 | So all debugfs users - at least those which can be built as modules - must | 
|  | 169 | be prepared to remove all files and directories they create there.  A file | 
|  | 170 | can be removed with: | 
|  | 171 |  | 
|  | 172 | void debugfs_remove(struct dentry *dentry); | 
|  | 173 |  | 
|  | 174 | The dentry value can be NULL, in which case nothing will be removed. | 
|  | 175 |  | 
|  | 176 | Once upon a time, debugfs users were required to remember the dentry | 
|  | 177 | pointer for every debugfs file they created so that all files could be | 
|  | 178 | cleaned up.  We live in more civilized times now, though, and debugfs users | 
|  | 179 | can call: | 
|  | 180 |  | 
|  | 181 | void debugfs_remove_recursive(struct dentry *dentry); | 
|  | 182 |  | 
|  | 183 | If this function is passed a pointer for the dentry corresponding to the | 
|  | 184 | top-level directory, the entire hierarchy below that directory will be | 
|  | 185 | removed. | 
|  | 186 |  | 
|  | 187 | Notes: | 
|  | 188 | [1] http://lwn.net/Articles/309298/ |