Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 .. include:: <isonum.txt>
0003 
0004 =======
0005 DebugFS
0006 =======
0007 
0008 Copyright |copy| 2009 Jonathan Corbet <corbet@lwn.net>
0009 
0010 Debugfs exists as a simple way for kernel developers to make information
0011 available to user space.  Unlike /proc, which is only meant for information
0012 about a process, or sysfs, which has strict one-value-per-file rules,
0013 debugfs has no rules at all.  Developers can put any information they want
0014 there.  The debugfs filesystem is also intended to not serve as a stable
0015 ABI to user space; in theory, there are no stability constraints placed on
0016 files exported there.  The real world is not always so simple, though [1]_;
0017 even debugfs interfaces are best designed with the idea that they will need
0018 to be maintained forever.
0019 
0020 Debugfs is typically mounted with a command like::
0021 
0022     mount -t debugfs none /sys/kernel/debug
0023 
0024 (Or an equivalent /etc/fstab line).
0025 The debugfs root directory is accessible only to the root user by
0026 default. To change access to the tree the "uid", "gid" and "mode" mount
0027 options can be used.
0028 
0029 Note that the debugfs API is exported GPL-only to modules.
0030 
0031 Code using debugfs should include <linux/debugfs.h>.  Then, the first order
0032 of business will be to create at least one directory to hold a set of
0033 debugfs files::
0034 
0035     struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
0036 
0037 This call, if successful, will make a directory called name underneath the
0038 indicated parent directory.  If parent is NULL, the directory will be
0039 created in the debugfs root.  On success, the return value is a struct
0040 dentry pointer which can be used to create files in the directory (and to
0041 clean it up at the end).  An ERR_PTR(-ERROR) return value indicates that
0042 something went wrong.  If ERR_PTR(-ENODEV) is returned, that is an
0043 indication that the kernel has been built without debugfs support and none
0044 of the functions described below will work.
0045 
0046 The most general way to create a file within a debugfs directory is with::
0047 
0048     struct dentry *debugfs_create_file(const char *name, umode_t mode,
0049                                        struct dentry *parent, void *data,
0050                                        const struct file_operations *fops);
0051 
0052 Here, name is the name of the file to create, mode describes the access
0053 permissions the file should have, parent indicates the directory which
0054 should hold the file, data will be stored in the i_private field of the
0055 resulting inode structure, and fops is a set of file operations which
0056 implement the file's behavior.  At a minimum, the read() and/or write()
0057 operations should be provided; others can be included as needed.  Again,
0058 the return value will be a dentry pointer to the created file,
0059 ERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is
0060 missing.
0061 
0062 Create a file with an initial size, the following function can be used
0063 instead::
0064 
0065     void debugfs_create_file_size(const char *name, umode_t mode,
0066                                   struct dentry *parent, void *data,
0067                                   const struct file_operations *fops,
0068                                   loff_t file_size);
0069 
0070 file_size is the initial file size. The other parameters are the same
0071 as the function debugfs_create_file.
0072 
0073 In a number of cases, the creation of a set of file operations is not
0074 actually necessary; the debugfs code provides a number of helper functions
0075 for simple situations.  Files containing a single integer value can be
0076 created with any of::
0077 
0078     void debugfs_create_u8(const char *name, umode_t mode,
0079                            struct dentry *parent, u8 *value);
0080     void debugfs_create_u16(const char *name, umode_t mode,
0081                             struct dentry *parent, u16 *value);
0082     void debugfs_create_u32(const char *name, umode_t mode,
0083                             struct dentry *parent, u32 *value);
0084     void debugfs_create_u64(const char *name, umode_t mode,
0085                             struct dentry *parent, u64 *value);
0086 
0087 These files support both reading and writing the given value; if a specific
0088 file should not be written to, simply set the mode bits accordingly.  The
0089 values in these files are in decimal; if hexadecimal is more appropriate,
0090 the following functions can be used instead::
0091 
0092     void debugfs_create_x8(const char *name, umode_t mode,
0093                            struct dentry *parent, u8 *value);
0094     void debugfs_create_x16(const char *name, umode_t mode,
0095                             struct dentry *parent, u16 *value);
0096     void debugfs_create_x32(const char *name, umode_t mode,
0097                             struct dentry *parent, u32 *value);
0098     void debugfs_create_x64(const char *name, umode_t mode,
0099                             struct dentry *parent, u64 *value);
0100 
0101 These functions are useful as long as the developer knows the size of the
0102 value to be exported.  Some types can have different widths on different
0103 architectures, though, complicating the situation somewhat.  There are
0104 functions meant to help out in such special cases::
0105 
0106     void debugfs_create_size_t(const char *name, umode_t mode,
0107                                struct dentry *parent, size_t *value);
0108 
0109 As might be expected, this function will create a debugfs file to represent
0110 a variable of type size_t.
0111 
0112 Similarly, there are helpers for variables of type unsigned long, in decimal
0113 and hexadecimal::
0114 
0115     struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
0116                                         struct dentry *parent,
0117                                         unsigned long *value);
0118     void debugfs_create_xul(const char *name, umode_t mode,
0119                             struct dentry *parent, unsigned long *value);
0120 
0121 Boolean values can be placed in debugfs with::
0122 
0123     void debugfs_create_bool(const char *name, umode_t mode,
0124                              struct dentry *parent, bool *value);
0125 
0126 A read on the resulting file will yield either Y (for non-zero values) or
0127 N, followed by a newline.  If written to, it will accept either upper- or
0128 lower-case values, or 1 or 0.  Any other input will be silently ignored.
0129 
0130 Also, atomic_t values can be placed in debugfs with::
0131 
0132     void debugfs_create_atomic_t(const char *name, umode_t mode,
0133                                  struct dentry *parent, atomic_t *value)
0134 
0135 A read of this file will get atomic_t values, and a write of this file
0136 will set atomic_t values.
0137 
0138 Another option is exporting a block of arbitrary binary data, with
0139 this structure and function::
0140 
0141     struct debugfs_blob_wrapper {
0142         void *data;
0143         unsigned long size;
0144     };
0145 
0146     struct dentry *debugfs_create_blob(const char *name, umode_t mode,
0147                                        struct dentry *parent,
0148                                        struct debugfs_blob_wrapper *blob);
0149 
0150 A read of this file will return the data pointed to by the
0151 debugfs_blob_wrapper structure.  Some drivers use "blobs" as a simple way
0152 to return several lines of (static) formatted text output.  This function
0153 can be used to export binary information, but there does not appear to be
0154 any code which does so in the mainline.  Note that all files created with
0155 debugfs_create_blob() are read-only.
0156 
0157 If you want to dump a block of registers (something that happens quite
0158 often during development, even if little such code reaches mainline.
0159 Debugfs offers two functions: one to make a registers-only file, and
0160 another to insert a register block in the middle of another sequential
0161 file::
0162 
0163     struct debugfs_reg32 {
0164         char *name;
0165         unsigned long offset;
0166     };
0167 
0168     struct debugfs_regset32 {
0169         const struct debugfs_reg32 *regs;
0170         int nregs;
0171         void __iomem *base;
0172         struct device *dev;     /* Optional device for Runtime PM */
0173     };
0174 
0175     debugfs_create_regset32(const char *name, umode_t mode,
0176                             struct dentry *parent,
0177                             struct debugfs_regset32 *regset);
0178 
0179     void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
0180                          int nregs, void __iomem *base, char *prefix);
0181 
0182 The "base" argument may be 0, but you may want to build the reg32 array
0183 using __stringify, and a number of register names (macros) are actually
0184 byte offsets over a base for the register block.
0185 
0186 If you want to dump an u32 array in debugfs, you can create file with::
0187 
0188     struct debugfs_u32_array {
0189         u32 *array;
0190         u32 n_elements;
0191     };
0192 
0193     void debugfs_create_u32_array(const char *name, umode_t mode,
0194                         struct dentry *parent,
0195                         struct debugfs_u32_array *array);
0196 
0197 The "array" argument wraps a pointer to the array's data and the number
0198 of its elements. Note: Once array is created its size can not be changed.
0199 
0200 There is a helper function to create device related seq_file::
0201 
0202    void debugfs_create_devm_seqfile(struct device *dev,
0203                                 const char *name,
0204                                 struct dentry *parent,
0205                                 int (*read_fn)(struct seq_file *s,
0206                                         void *data));
0207 
0208 The "dev" argument is the device related to this debugfs file, and
0209 the "read_fn" is a function pointer which to be called to print the
0210 seq_file content.
0211 
0212 There are a couple of other directory-oriented helper functions::
0213 
0214     struct dentry *debugfs_rename(struct dentry *old_dir,
0215                                   struct dentry *old_dentry,
0216                                   struct dentry *new_dir,
0217                                   const char *new_name);
0218 
0219     struct dentry *debugfs_create_symlink(const char *name,
0220                                           struct dentry *parent,
0221                                           const char *target);
0222 
0223 A call to debugfs_rename() will give a new name to an existing debugfs
0224 file, possibly in a different directory.  The new_name must not exist prior
0225 to the call; the return value is old_dentry with updated information.
0226 Symbolic links can be created with debugfs_create_symlink().
0227 
0228 There is one important thing that all debugfs users must take into account:
0229 there is no automatic cleanup of any directories created in debugfs.  If a
0230 module is unloaded without explicitly removing debugfs entries, the result
0231 will be a lot of stale pointers and no end of highly antisocial behavior.
0232 So all debugfs users - at least those which can be built as modules - must
0233 be prepared to remove all files and directories they create there.  A file
0234 can be removed with::
0235 
0236     void debugfs_remove(struct dentry *dentry);
0237 
0238 The dentry value can be NULL or an error value, in which case nothing will
0239 be removed.
0240 
0241 Once upon a time, debugfs users were required to remember the dentry
0242 pointer for every debugfs file they created so that all files could be
0243 cleaned up.  We live in more civilized times now, though, and debugfs users
0244 can call::
0245 
0246     void debugfs_remove_recursive(struct dentry *dentry);
0247 
0248 If this function is passed a pointer for the dentry corresponding to the
0249 top-level directory, the entire hierarchy below that directory will be
0250 removed.
0251 
0252 .. [1] http://lwn.net/Articles/309298/