DebugFS

Copyright © 2009 Jonathan Corbet <corbet@lwn.net>

Debugfs exists as a simple way for kernel developers to make informationavailable to user space. Unlike /proc, which is only meant for informationabout a process, or sysfs, which has strict one-value-per-file rules,debugfs has no rules at all. Developers can put any information they wantthere. The debugfs filesystem is also intended to not serve as a stableABI to user space; in theory, there are no stability constraints placed onfiles exported there. The real world is not always so simple, though[1];even debugfs interfaces are best designed with the idea that they will needto be maintained forever.

Debugfs is typically mounted with a command like:

mount -t debugfs none /sys/kernel/debug

(Or an equivalent /etc/fstab line).The debugfs root directory is accessible only to the root user bydefault. To change access to the tree the “uid”, “gid” and “mode” mountoptions can be used.

Note that the debugfs API is exported GPL-only to modules.

Code using debugfs should include <linux/debugfs.h>. Then, the first orderof business will be to create at least one directory to hold a set ofdebugfs files:

struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);

This call, if successful, will make a directory called name underneath theindicated parent directory. If parent is NULL, the directory will becreated in the debugfs root. On success, the return value is astructdentry pointer which can be used to create files in the directory (and toclean it up at the end). An ERR_PTR(-ERROR) return value indicates thatsomething went wrong. If ERR_PTR(-ENODEV) is returned, that is anindication that the kernel has been built without debugfs support and noneof the functions described below will work.

The most general way to create a file within a debugfs directory is with:

struct dentry *debugfs_create_file(const char *name, umode_t mode,                                   struct dentry *parent, void *data,                                   const struct file_operations *fops);

Here, name is the name of the file to create, mode describes the accesspermissions the file should have, parent indicates the directory whichshould hold the file, data will be stored in the i_private field of theresulting inode structure, and fops is a set of file operations whichimplement the file’s behavior. At a minimum, the read() and/or write()operations should be provided; others can be included as needed. Again,the return value will be a dentry pointer to the created file,ERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support ismissing.

Create a file with an initial size, the following function can be usedinstead:

void debugfs_create_file_size(const char *name, umode_t mode,                              struct dentry *parent, void *data,                              const struct file_operations *fops,                              loff_t file_size);

file_size is the initial file size. The other parameters are the sameas the function debugfs_create_file.

In a number of cases, the creation of a set of file operations is notactually necessary; the debugfs code provides a number of helper functionsfor simple situations. Files containing a single integer value can becreated with any of:

void debugfs_create_u8(const char *name, umode_t mode,                       struct dentry *parent, u8 *value);void debugfs_create_u16(const char *name, umode_t mode,                        struct dentry *parent, u16 *value);void debugfs_create_u32(const char *name, umode_t mode,                        struct dentry *parent, u32 *value);void debugfs_create_u64(const char *name, umode_t mode,                        struct dentry *parent, u64 *value);

These files support both reading and writing the given value; if a specificfile should not be written to, simply set the mode bits accordingly. Thevalues in these files are in decimal; if hexadecimal is more appropriate,the following functions can be used instead:

void debugfs_create_x8(const char *name, umode_t mode,                       struct dentry *parent, u8 *value);void debugfs_create_x16(const char *name, umode_t mode,                        struct dentry *parent, u16 *value);void debugfs_create_x32(const char *name, umode_t mode,                        struct dentry *parent, u32 *value);void debugfs_create_x64(const char *name, umode_t mode,                        struct dentry *parent, u64 *value);

These functions are useful as long as the developer knows the size of thevalue to be exported. Some types can have different widths on differentarchitectures, though, complicating the situation somewhat. There arefunctions meant to help out in such special cases:

void debugfs_create_size_t(const char *name, umode_t mode,                           struct dentry *parent, size_t *value);

As might be expected, this function will create a debugfs file to representa variable of type size_t.

Similarly, there are helpers for variables of type unsigned long, in decimaland hexadecimal:

struct dentry *debugfs_create_ulong(const char *name, umode_t mode,                                    struct dentry *parent,                                    unsigned long *value);void debugfs_create_xul(const char *name, umode_t mode,                        struct dentry *parent, unsigned long *value);

Boolean values can be placed in debugfs with:

void debugfs_create_bool(const char *name, umode_t mode,                         struct dentry *parent, bool *value);

A read on the resulting file will yield either Y (for non-zero values) orN, followed by a newline. If written to, it will accept either upper- orlower-case values, or 1 or 0. Any other input will be silently ignored.

Also, atomic_t values can be placed in debugfs with:

void debugfs_create_atomic_t(const char *name, umode_t mode,                             struct dentry *parent, atomic_t *value)

A read of this file will get atomic_t values, and a write of this filewill set atomic_t values.

Another option is exporting a block of arbitrary binary data, withthis structure and function:

struct debugfs_blob_wrapper {    void *data;    unsigned long size;};struct dentry *debugfs_create_blob(const char *name, umode_t mode,                                   struct dentry *parent,                                   struct debugfs_blob_wrapper *blob);

A read of this file will return the data pointed to by thedebugfs_blob_wrapper structure. Some drivers use “blobs” as a simple wayto return several lines of (static) formatted text output. This functioncan be used to export binary information, but there does not appear to beany code which does so in the mainline. Note that all files created withdebugfs_create_blob() are read-only.

If you want to dump a block of registers (something that happens quiteoften during development, even if little such code reaches mainline),debugfs offers two functions: one to make a registers-only file, andanother to insert a register block in the middle of another sequentialfile:

struct debugfs_reg32 {    char *name;    unsigned long offset;};struct debugfs_regset32 {    const struct debugfs_reg32 *regs;    int nregs;    void __iomem *base;    struct device *dev;     /* Optional device for Runtime PM */};debugfs_create_regset32(const char *name, umode_t mode,                        struct dentry *parent,                        struct debugfs_regset32 *regset);void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,                     int nregs, void __iomem *base, char *prefix);

The “base” argument may be 0, but you may want to build the reg32 arrayusing __stringify, and a number of register names (macros) are actuallybyte offsets over a base for the register block.

If you want to dump a u32 array in debugfs, you can create a file with:

struct debugfs_u32_array {    u32 *array;    u32 n_elements;};void debugfs_create_u32_array(const char *name, umode_t mode,                    struct dentry *parent,                    struct debugfs_u32_array *array);

The “array” argument wraps a pointer to the array’s data and the numberof its elements. Note: Once array is created its size can not be changed.

There is a helper function to create a device-related seq_file:

void debugfs_create_devm_seqfile(struct device *dev,                             const char *name,                             struct dentry *parent,                             int (*read_fn)(struct seq_file *s,                                     void *data));

The “dev” argument is the device related to this debugfs file, andthe “read_fn” is a function pointer which to be called to print theseq_file content.

There are a couple of other directory-oriented helper functions:

struct dentry *debugfs_change_name(struct dentry *dentry,                                      const char *fmt, ...);struct dentry *debugfs_create_symlink(const char *name,                                      struct dentry *parent,                                      const char *target);

A call todebugfs_change_name() will give a new name to an existing debugfsfile, always in the same directory. The new_name must not exist priorto the call; the return value is 0 on success and -E... on failure.Symbolic links can be created withdebugfs_create_symlink().

There is one important thing that all debugfs users must take into account:there is no automatic cleanup of any directories created in debugfs. If amodule is unloaded without explicitly removing debugfs entries, the resultwill be a lot of stale pointers and no end of highly antisocial behavior.So all debugfs users - at least those which can be built as modules - mustbe prepared to remove all files and directories they create there. A fileor directory can be removed with:

void debugfs_remove(struct dentry *dentry);

The dentry value can be NULL or an error value, in which case nothing willbe removed. Note that this function will recursively remove all files anddirectories underneath it. Previously,debugfs_remove_recursive() was usedto perform that task, but this function is now just an alias todebugfs_remove().debugfs_remove_recursive() should be considereddeprecated.

[1]

http://lwn.net/Articles/309298/