blob: 9e27c843d00ea0602e391f760c05764066a544c0 [file] [log] [blame]
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -06001Copyright 2009 Jonathan Corbet <corbet@lwn.net>
2
3Debugfs exists as a simple way for kernel developers to make information
4available to user space. Unlike /proc, which is only meant for information
5about a process, or sysfs, which has strict one-value-per-file rules,
6debugfs has no rules at all. Developers can put any information they want
7there. The debugfs filesystem is also intended to not serve as a stable
8ABI to user space; in theory, there are no stability constraints placed on
9files exported there. The real world is not always so simple, though [1];
10even debugfs interfaces are best designed with the idea that they will need
11to be maintained forever.
12
13Debugfs is typically mounted with a command like:
14
15 mount -t debugfs none /sys/kernel/debug
16
Ludwig Nusseld6e48682012-01-25 11:52:28 +010017(Or an equivalent /etc/fstab line).
Kees Cook82aceae42012-08-27 13:32:15 -070018The debugfs root directory is accessible only to the root user by
19default. To change access to the tree the "uid", "gid" and "mode" mount
Ludwig Nusseld6e48682012-01-25 11:52:28 +010020options can be used.
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060021
22Note that the debugfs API is exported GPL-only to modules.
23
24Code using debugfs should include <linux/debugfs.h>. Then, the first order
25of business will be to create at least one directory to hold a set of
26debugfs files:
27
28 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
29
30This call, if successful, will make a directory called name underneath the
31indicated parent directory. If parent is NULL, the directory will be
32created in the debugfs root. On success, the return value is a struct
33dentry pointer which can be used to create files in the directory (and to
Ronald Tschalär9abb2492019-04-15 01:25:05 -070034clean it up at the end). An ERR_PTR(-ERROR) return value indicates that
35something went wrong. If ERR_PTR(-ENODEV) is returned, that is an
36indication that the kernel has been built without debugfs support and none
37of the functions described below will work.
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060038
39The most general way to create a file within a debugfs directory is with:
40
Al Virof4ae40a62011-07-24 04:33:43 -040041 struct dentry *debugfs_create_file(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060042 struct dentry *parent, void *data,
43 const struct file_operations *fops);
44
45Here, name is the name of the file to create, mode describes the access
46permissions the file should have, parent indicates the directory which
47should hold the file, data will be stored in the i_private field of the
48resulting inode structure, and fops is a set of file operations which
49implement the file's behavior. At a minimum, the read() and/or write()
50operations should be provided; others can be included as needed. Again,
Ronald Tschalär9abb2492019-04-15 01:25:05 -070051the return value will be a dentry pointer to the created file,
52ERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is
53missing.
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060054
Wang Long9e1aa7c2015-07-16 06:31:16 +000055Create a file with an initial size, the following function can be used
56instead:
57
58 struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
59 struct dentry *parent, void *data,
60 const struct file_operations *fops,
61 loff_t file_size);
62
63file_size is the initial file size. The other parameters are the same
64as the function debugfs_create_file.
65
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060066In a number of cases, the creation of a set of file operations is not
67actually necessary; the debugfs code provides a number of helper functions
68for simple situations. Files containing a single integer value can be
69created with any of:
70
Al Virof4ae40a62011-07-24 04:33:43 -040071 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060072 struct dentry *parent, u8 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040073 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060074 struct dentry *parent, u16 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040075 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060076 struct dentry *parent, u32 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040077 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060078 struct dentry *parent, u64 *value);
79
80These files support both reading and writing the given value; if a specific
81file should not be written to, simply set the mode bits accordingly. The
82values in these files are in decimal; if hexadecimal is more appropriate,
83the following functions can be used instead:
84
Al Virof4ae40a62011-07-24 04:33:43 -040085 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060086 struct dentry *parent, u8 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040087 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060088 struct dentry *parent, u16 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040089 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060090 struct dentry *parent, u32 *value);
Al Virof4ae40a62011-07-24 04:33:43 -040091 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Akinobu Mitad0a54262011-07-09 14:01:17 +090092 struct dentry *parent, u64 *value);
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -060093
94These functions are useful as long as the developer knows the size of the
95value to be exported. Some types can have different widths on different
96architectures, though, complicating the situation somewhat. There is a
97function meant to help out in one special case:
98
Al Virof4ae40a62011-07-24 04:33:43 -040099 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600100 struct dentry *parent,
101 size_t *value);
102
103As might be expected, this function will create a debugfs file to represent
104a variable of type size_t.
105
106Boolean values can be placed in debugfs with:
107
Al Virof4ae40a62011-07-24 04:33:43 -0400108 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700109 struct dentry *parent, bool *value);
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600110
111A read on the resulting file will yield either Y (for non-zero values) or
112N, followed by a newline. If written to, it will accept either upper- or
113lower-case values, or 1 or 0. Any other input will be silently ignored.
114
Wang Long9e1aa7c2015-07-16 06:31:16 +0000115Also, atomic_t values can be placed in debugfs with:
116
117 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
118 struct dentry *parent, atomic_t *value)
119
120A read of this file will get atomic_t values, and a write of this file
121will set atomic_t values.
122
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100123Another option is exporting a block of arbitrary binary data, with
124this structure and function:
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600125
126 struct debugfs_blob_wrapper {
127 void *data;
128 unsigned long size;
129 };
130
Al Virof4ae40a62011-07-24 04:33:43 -0400131 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600132 struct dentry *parent,
133 struct debugfs_blob_wrapper *blob);
134
135A read of this file will return the data pointed to by the
136debugfs_blob_wrapper structure. Some drivers use "blobs" as a simple way
137to return several lines of (static) formatted text output. This function
138can be used to export binary information, but there does not appear to be
139any code which does so in the mainline. Note that all files created with
140debugfs_create_blob() are read-only.
141
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100142If you want to dump a block of registers (something that happens quite
143often during development, even if little such code reaches mainline.
144Debugfs offers two functions: one to make a registers-only file, and
145another to insert a register block in the middle of another sequential
146file.
147
148 struct debugfs_reg32 {
149 char *name;
150 unsigned long offset;
151 };
152
153 struct debugfs_regset32 {
154 struct debugfs_reg32 *regs;
155 int nregs;
156 void __iomem *base;
157 };
158
Al Viro88187392012-03-20 06:00:24 -0400159 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100160 struct dentry *parent,
161 struct debugfs_regset32 *regset);
162
Joe Perches97615362014-09-29 16:08:26 -0700163 void debugfs_print_regs32(struct seq_file *s, struct debugfs_reg32 *regs,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100164 int nregs, void __iomem *base, char *prefix);
165
166The "base" argument may be 0, but you may want to build the reg32 array
167using __stringify, and a number of register names (macros) are actually
168byte offsets over a base for the register block.
169
Wang Long9e1aa7c2015-07-16 06:31:16 +0000170If you want to dump an u32 array in debugfs, you can create file with:
171
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +0200172 void debugfs_create_u32_array(const char *name, umode_t mode,
Wang Long9e1aa7c2015-07-16 06:31:16 +0000173 struct dentry *parent,
174 u32 *array, u32 elements);
175
176The "array" argument provides data, and the "elements" argument is
177the number of elements in the array. Note: Once array is created its
178size can not be changed.
179
180There is a helper function to create device related seq_file:
181
182 struct dentry *debugfs_create_devm_seqfile(struct device *dev,
183 const char *name,
184 struct dentry *parent,
185 int (*read_fn)(struct seq_file *s,
186 void *data));
187
188The "dev" argument is the device related to this debugfs file, and
189the "read_fn" is a function pointer which to be called to print the
190seq_file content.
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100191
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600192There are a couple of other directory-oriented helper functions:
193
194 struct dentry *debugfs_rename(struct dentry *old_dir,
195 struct dentry *old_dentry,
196 struct dentry *new_dir,
197 const char *new_name);
198
199 struct dentry *debugfs_create_symlink(const char *name,
200 struct dentry *parent,
201 const char *target);
202
203A call to debugfs_rename() will give a new name to an existing debugfs
204file, possibly in a different directory. The new_name must not exist prior
205to the call; the return value is old_dentry with updated information.
206Symbolic links can be created with debugfs_create_symlink().
207
208There is one important thing that all debugfs users must take into account:
209there is no automatic cleanup of any directories created in debugfs. If a
210module is unloaded without explicitly removing debugfs entries, the result
211will be a lot of stale pointers and no end of highly antisocial behavior.
212So all debugfs users - at least those which can be built as modules - must
213be prepared to remove all files and directories they create there. A file
214can be removed with:
215
216 void debugfs_remove(struct dentry *dentry);
217
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700218The dentry value can be NULL or an error value, in which case nothing will
219be removed.
Jonathan Corbetf89d7ea2009-06-04 16:35:25 -0600220
221Once upon a time, debugfs users were required to remember the dentry
222pointer for every debugfs file they created so that all files could be
223cleaned up. We live in more civilized times now, though, and debugfs users
224can call:
225
226 void debugfs_remove_recursive(struct dentry *dentry);
227
228If this function is passed a pointer for the dentry corresponding to the
229top-level directory, the entire hierarchy below that directory will be
230removed.
231
232Notes:
233 [1] http://lwn.net/Articles/309298/