001 Copyright 2009 Jonathan Corbet <corbet@lwn.net>
002
003 Debugfs exists as a simple way for kernel developers to make information
004 available to user space. Unlike /proc, which is only meant for information
005 about a process, or sysfs, which has strict one-value-per-file rules,
006 debugfs has no rules at all. Developers can put any information they want
007 there. The debugfs filesystem is also intended to not serve as a stable
008 ABI to user space; in theory, there are no stability constraints placed on
009 files exported there. The real world is not always so simple, though [1];
010 even debugfs interfaces are best designed with the idea that they will need
011 to be maintained forever.
012
013 Debugfs is typically mounted with a command like:
014
015 mount -t debugfs none /sys/kernel/debug
016
017 (Or an equivalent /etc/fstab line).
018
019 Note that the debugfs API is exported GPL-only to modules.
020
021 Code using debugfs should include <linux/debugfs.h>. Then, the first order
022 of business will be to create at least one directory to hold a set of
023 debugfs files:
024
025 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
026
027 This call, if successful, will make a directory called name underneath the
028 indicated parent directory. If parent is NULL, the directory will be
029 created in the debugfs root. On success, the return value is a struct
030 dentry pointer which can be used to create files in the directory (and to
031 clean it up at the end). A NULL return value indicates that something went
032 wrong. If ERR_PTR(-ENODEV) is returned, that is an indication that the
033 kernel has been built without debugfs support and none of the functions
034 described below will work.
035
036 The most general way to create a file within a debugfs directory is with:
037
038 struct dentry *debugfs_create_file(const char *name, mode_t mode,
039 struct dentry *parent, void *data,
040 const struct file_operations *fops);
041
042 Here, name is the name of the file to create, mode describes the access
043 permissions the file should have, parent indicates the directory which
044 should hold the file, data will be stored in the i_private field of the
045 resulting inode structure, and fops is a set of file operations which
046 implement the file's behavior. At a minimum, the read() and/or write()
047 operations should be provided; others can be included as needed. Again,
048 the return value will be a dentry pointer to the created file, NULL for
049 error, or ERR_PTR(-ENODEV) if debugfs support is missing.
050
051 In a number of cases, the creation of a set of file operations is not
052 actually necessary; the debugfs code provides a number of helper functions
053 for simple situations. Files containing a single integer value can be
054 created with any of:
055
056 struct dentry *debugfs_create_u8(const char *name, mode_t mode,
057 struct dentry *parent, u8 *value);
058 struct dentry *debugfs_create_u16(const char *name, mode_t mode,
059 struct dentry *parent, u16 *value);
060 struct dentry *debugfs_create_u32(const char *name, mode_t mode,
061 struct dentry *parent, u32 *value);
062 struct dentry *debugfs_create_u64(const char *name, mode_t mode,
063 struct dentry *parent, u64 *value);
064
065 These files support both reading and writing the given value; if a specific
066 file should not be written to, simply set the mode bits accordingly. The
067 values in these files are in decimal; if hexadecimal is more appropriate,
068 the following functions can be used instead:
069
070 struct dentry *debugfs_create_x8(const char *name, mode_t mode,
071 struct dentry *parent, u8 *value);
072 struct dentry *debugfs_create_x16(const char *name, mode_t mode,
073 struct dentry *parent, u16 *value);
074 struct dentry *debugfs_create_x32(const char *name, mode_t mode,
075 struct dentry *parent, u32 *value);
076
077 Note that there is no debugfs_create_x64().
078
079 These functions are useful as long as the developer knows the size of the
080 value to be exported. Some types can have different widths on different
081 architectures, though, complicating the situation somewhat. There is a
082 function meant to help out in one special case:
083
084 struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
085 struct dentry *parent,
086 size_t *value);
087
088 As might be expected, this function will create a debugfs file to represent
089 a variable of type size_t.
090
091 Boolean values can be placed in debugfs with:
092
093 struct dentry *debugfs_create_bool(const char *name, mode_t mode,
094 struct dentry *parent, u32 *value);
095
096 A read on the resulting file will yield either Y (for non-zero values) or
097 N, followed by a newline. If written to, it will accept either upper- or
098 lower-case values, or 1 or 0. Any other input will be silently ignored.
099
100 Finally, a block of arbitrary binary data can be exported with:
101
102 struct debugfs_blob_wrapper {
103 void *data;
104 unsigned long size;
105 };
106
107 struct dentry *debugfs_create_blob(const char *name, mode_t mode,
108 struct dentry *parent,
109 struct debugfs_blob_wrapper *blob);
110
111 A read of this file will return the data pointed to by the
112 debugfs_blob_wrapper structure. Some drivers use "blobs" as a simple way
113 to return several lines of (static) formatted text output. This function
114 can be used to export binary information, but there does not appear to be
115 any code which does so in the mainline. Note that all files created with
116 debugfs_create_blob() are read-only.
117
118 There are a couple of other directory-oriented helper functions:
119
120 struct dentry *debugfs_rename(struct dentry *old_dir,
121 struct dentry *old_dentry,
122 struct dentry *new_dir,
123 const char *new_name);
124
125 struct dentry *debugfs_create_symlink(const char *name,
126 struct dentry *parent,
127 const char *target);
128
129 A call to debugfs_rename() will give a new name to an existing debugfs
130 file, possibly in a different directory. The new_name must not exist prior
131 to the call; the return value is old_dentry with updated information.
132 Symbolic links can be created with debugfs_create_symlink().
133
134 There is one important thing that all debugfs users must take into account:
135 there is no automatic cleanup of any directories created in debugfs. If a
136 module is unloaded without explicitly removing debugfs entries, the result
137 will be a lot of stale pointers and no end of highly antisocial behavior.
138 So all debugfs users - at least those which can be built as modules - must
139 be prepared to remove all files and directories they create there. A file
140 can be removed with:
141
142 void debugfs_remove(struct dentry *dentry);
143
144 The dentry value can be NULL, in which case nothing will be removed.
145
146 Once upon a time, debugfs users were required to remember the dentry
147 pointer for every debugfs file they created so that all files could be
148 cleaned up. We live in more civilized times now, though, and debugfs users
149 can call:
150
151 void debugfs_remove_recursive(struct dentry *dentry);
152
153 If this function is passed a pointer for the dentry corresponding to the
154 top-level directory, the entire hierarchy below that directory will be
155 removed.
156
157 Notes:
158 [1] http://lwn.net/Articles/309298/