1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun.. include:: <isonum.txt> 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun======= 5*4882a593SmuzhiyunDebugFS 6*4882a593Smuzhiyun======= 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunCopyright |copy| 2009 Jonathan Corbet <corbet@lwn.net> 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunDebugfs exists as a simple way for kernel developers to make information 11*4882a593Smuzhiyunavailable to user space. Unlike /proc, which is only meant for information 12*4882a593Smuzhiyunabout a process, or sysfs, which has strict one-value-per-file rules, 13*4882a593Smuzhiyundebugfs has no rules at all. Developers can put any information they want 14*4882a593Smuzhiyunthere. The debugfs filesystem is also intended to not serve as a stable 15*4882a593SmuzhiyunABI to user space; in theory, there are no stability constraints placed on 16*4882a593Smuzhiyunfiles exported there. The real world is not always so simple, though [1]_; 17*4882a593Smuzhiyuneven debugfs interfaces are best designed with the idea that they will need 18*4882a593Smuzhiyunto be maintained forever. 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunDebugfs is typically mounted with a command like:: 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun mount -t debugfs none /sys/kernel/debug 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun(Or an equivalent /etc/fstab line). 25*4882a593SmuzhiyunThe debugfs root directory is accessible only to the root user by 26*4882a593Smuzhiyundefault. To change access to the tree the "uid", "gid" and "mode" mount 27*4882a593Smuzhiyunoptions can be used. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunNote that the debugfs API is exported GPL-only to modules. 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunCode using debugfs should include <linux/debugfs.h>. Then, the first order 32*4882a593Smuzhiyunof business will be to create at least one directory to hold a set of 33*4882a593Smuzhiyundebugfs files:: 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunThis call, if successful, will make a directory called name underneath the 38*4882a593Smuzhiyunindicated parent directory. If parent is NULL, the directory will be 39*4882a593Smuzhiyuncreated in the debugfs root. On success, the return value is a struct 40*4882a593Smuzhiyundentry pointer which can be used to create files in the directory (and to 41*4882a593Smuzhiyunclean it up at the end). An ERR_PTR(-ERROR) return value indicates that 42*4882a593Smuzhiyunsomething went wrong. If ERR_PTR(-ENODEV) is returned, that is an 43*4882a593Smuzhiyunindication that the kernel has been built without debugfs support and none 44*4882a593Smuzhiyunof the functions described below will work. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunThe most general way to create a file within a debugfs directory is with:: 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun struct dentry *debugfs_create_file(const char *name, umode_t mode, 49*4882a593Smuzhiyun struct dentry *parent, void *data, 50*4882a593Smuzhiyun const struct file_operations *fops); 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunHere, name is the name of the file to create, mode describes the access 53*4882a593Smuzhiyunpermissions the file should have, parent indicates the directory which 54*4882a593Smuzhiyunshould hold the file, data will be stored in the i_private field of the 55*4882a593Smuzhiyunresulting inode structure, and fops is a set of file operations which 56*4882a593Smuzhiyunimplement the file's behavior. At a minimum, the read() and/or write() 57*4882a593Smuzhiyunoperations should be provided; others can be included as needed. Again, 58*4882a593Smuzhiyunthe return value will be a dentry pointer to the created file, 59*4882a593SmuzhiyunERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is 60*4882a593Smuzhiyunmissing. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunCreate a file with an initial size, the following function can be used 63*4882a593Smuzhiyuninstead:: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun void debugfs_create_file_size(const char *name, umode_t mode, 66*4882a593Smuzhiyun struct dentry *parent, void *data, 67*4882a593Smuzhiyun const struct file_operations *fops, 68*4882a593Smuzhiyun loff_t file_size); 69*4882a593Smuzhiyun 70*4882a593Smuzhiyunfile_size is the initial file size. The other parameters are the same 71*4882a593Smuzhiyunas the function debugfs_create_file. 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunIn a number of cases, the creation of a set of file operations is not 74*4882a593Smuzhiyunactually necessary; the debugfs code provides a number of helper functions 75*4882a593Smuzhiyunfor simple situations. Files containing a single integer value can be 76*4882a593Smuzhiyuncreated with any of:: 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun void debugfs_create_u8(const char *name, umode_t mode, 79*4882a593Smuzhiyun struct dentry *parent, u8 *value); 80*4882a593Smuzhiyun void debugfs_create_u16(const char *name, umode_t mode, 81*4882a593Smuzhiyun struct dentry *parent, u16 *value); 82*4882a593Smuzhiyun void debugfs_create_u32(const char *name, umode_t mode, 83*4882a593Smuzhiyun struct dentry *parent, u32 *value); 84*4882a593Smuzhiyun void debugfs_create_u64(const char *name, umode_t mode, 85*4882a593Smuzhiyun struct dentry *parent, u64 *value); 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunThese files support both reading and writing the given value; if a specific 88*4882a593Smuzhiyunfile should not be written to, simply set the mode bits accordingly. The 89*4882a593Smuzhiyunvalues in these files are in decimal; if hexadecimal is more appropriate, 90*4882a593Smuzhiyunthe following functions can be used instead:: 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun void debugfs_create_x8(const char *name, umode_t mode, 93*4882a593Smuzhiyun struct dentry *parent, u8 *value); 94*4882a593Smuzhiyun void debugfs_create_x16(const char *name, umode_t mode, 95*4882a593Smuzhiyun struct dentry *parent, u16 *value); 96*4882a593Smuzhiyun void debugfs_create_x32(const char *name, umode_t mode, 97*4882a593Smuzhiyun struct dentry *parent, u32 *value); 98*4882a593Smuzhiyun void debugfs_create_x64(const char *name, umode_t mode, 99*4882a593Smuzhiyun struct dentry *parent, u64 *value); 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThese functions are useful as long as the developer knows the size of the 102*4882a593Smuzhiyunvalue to be exported. Some types can have different widths on different 103*4882a593Smuzhiyunarchitectures, though, complicating the situation somewhat. There are 104*4882a593Smuzhiyunfunctions meant to help out in such special cases:: 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun void debugfs_create_size_t(const char *name, umode_t mode, 107*4882a593Smuzhiyun struct dentry *parent, size_t *value); 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunAs might be expected, this function will create a debugfs file to represent 110*4882a593Smuzhiyuna variable of type size_t. 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunSimilarly, there are helpers for variables of type unsigned long, in decimal 113*4882a593Smuzhiyunand hexadecimal:: 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun struct dentry *debugfs_create_ulong(const char *name, umode_t mode, 116*4882a593Smuzhiyun struct dentry *parent, 117*4882a593Smuzhiyun unsigned long *value); 118*4882a593Smuzhiyun void debugfs_create_xul(const char *name, umode_t mode, 119*4882a593Smuzhiyun struct dentry *parent, unsigned long *value); 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunBoolean values can be placed in debugfs with:: 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun struct dentry *debugfs_create_bool(const char *name, umode_t mode, 124*4882a593Smuzhiyun struct dentry *parent, bool *value); 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunA read on the resulting file will yield either Y (for non-zero values) or 127*4882a593SmuzhiyunN, followed by a newline. If written to, it will accept either upper- or 128*4882a593Smuzhiyunlower-case values, or 1 or 0. Any other input will be silently ignored. 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunAlso, atomic_t values can be placed in debugfs with:: 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun void debugfs_create_atomic_t(const char *name, umode_t mode, 133*4882a593Smuzhiyun struct dentry *parent, atomic_t *value) 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunA read of this file will get atomic_t values, and a write of this file 136*4882a593Smuzhiyunwill set atomic_t values. 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunAnother option is exporting a block of arbitrary binary data, with 139*4882a593Smuzhiyunthis structure and function:: 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun struct debugfs_blob_wrapper { 142*4882a593Smuzhiyun void *data; 143*4882a593Smuzhiyun unsigned long size; 144*4882a593Smuzhiyun }; 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun struct dentry *debugfs_create_blob(const char *name, umode_t mode, 147*4882a593Smuzhiyun struct dentry *parent, 148*4882a593Smuzhiyun struct debugfs_blob_wrapper *blob); 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunA read of this file will return the data pointed to by the 151*4882a593Smuzhiyundebugfs_blob_wrapper structure. Some drivers use "blobs" as a simple way 152*4882a593Smuzhiyunto return several lines of (static) formatted text output. This function 153*4882a593Smuzhiyuncan be used to export binary information, but there does not appear to be 154*4882a593Smuzhiyunany code which does so in the mainline. Note that all files created with 155*4882a593Smuzhiyundebugfs_create_blob() are read-only. 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunIf you want to dump a block of registers (something that happens quite 158*4882a593Smuzhiyunoften during development, even if little such code reaches mainline. 159*4882a593SmuzhiyunDebugfs offers two functions: one to make a registers-only file, and 160*4882a593Smuzhiyunanother to insert a register block in the middle of another sequential 161*4882a593Smuzhiyunfile:: 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun struct debugfs_reg32 { 164*4882a593Smuzhiyun char *name; 165*4882a593Smuzhiyun unsigned long offset; 166*4882a593Smuzhiyun }; 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun struct debugfs_regset32 { 169*4882a593Smuzhiyun const struct debugfs_reg32 *regs; 170*4882a593Smuzhiyun int nregs; 171*4882a593Smuzhiyun void __iomem *base; 172*4882a593Smuzhiyun struct device *dev; /* Optional device for Runtime PM */ 173*4882a593Smuzhiyun }; 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun debugfs_create_regset32(const char *name, umode_t mode, 176*4882a593Smuzhiyun struct dentry *parent, 177*4882a593Smuzhiyun struct debugfs_regset32 *regset); 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 180*4882a593Smuzhiyun int nregs, void __iomem *base, char *prefix); 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunThe "base" argument may be 0, but you may want to build the reg32 array 183*4882a593Smuzhiyunusing __stringify, and a number of register names (macros) are actually 184*4882a593Smuzhiyunbyte offsets over a base for the register block. 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunIf you want to dump an u32 array in debugfs, you can create file with:: 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun struct debugfs_u32_array { 189*4882a593Smuzhiyun u32 *array; 190*4882a593Smuzhiyun u32 n_elements; 191*4882a593Smuzhiyun }; 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun void debugfs_create_u32_array(const char *name, umode_t mode, 194*4882a593Smuzhiyun struct dentry *parent, 195*4882a593Smuzhiyun struct debugfs_u32_array *array); 196*4882a593Smuzhiyun 197*4882a593SmuzhiyunThe "array" argument wraps a pointer to the array's data and the number 198*4882a593Smuzhiyunof its elements. Note: Once array is created its size can not be changed. 199*4882a593Smuzhiyun 200*4882a593SmuzhiyunThere is a helper function to create device related seq_file:: 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun void debugfs_create_devm_seqfile(struct device *dev, 203*4882a593Smuzhiyun const char *name, 204*4882a593Smuzhiyun struct dentry *parent, 205*4882a593Smuzhiyun int (*read_fn)(struct seq_file *s, 206*4882a593Smuzhiyun void *data)); 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunThe "dev" argument is the device related to this debugfs file, and 209*4882a593Smuzhiyunthe "read_fn" is a function pointer which to be called to print the 210*4882a593Smuzhiyunseq_file content. 211*4882a593Smuzhiyun 212*4882a593SmuzhiyunThere are a couple of other directory-oriented helper functions:: 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun struct dentry *debugfs_rename(struct dentry *old_dir, 215*4882a593Smuzhiyun struct dentry *old_dentry, 216*4882a593Smuzhiyun struct dentry *new_dir, 217*4882a593Smuzhiyun const char *new_name); 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun struct dentry *debugfs_create_symlink(const char *name, 220*4882a593Smuzhiyun struct dentry *parent, 221*4882a593Smuzhiyun const char *target); 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunA call to debugfs_rename() will give a new name to an existing debugfs 224*4882a593Smuzhiyunfile, possibly in a different directory. The new_name must not exist prior 225*4882a593Smuzhiyunto the call; the return value is old_dentry with updated information. 226*4882a593SmuzhiyunSymbolic links can be created with debugfs_create_symlink(). 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunThere is one important thing that all debugfs users must take into account: 229*4882a593Smuzhiyunthere is no automatic cleanup of any directories created in debugfs. If a 230*4882a593Smuzhiyunmodule is unloaded without explicitly removing debugfs entries, the result 231*4882a593Smuzhiyunwill be a lot of stale pointers and no end of highly antisocial behavior. 232*4882a593SmuzhiyunSo all debugfs users - at least those which can be built as modules - must 233*4882a593Smuzhiyunbe prepared to remove all files and directories they create there. A file 234*4882a593Smuzhiyuncan be removed with:: 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun void debugfs_remove(struct dentry *dentry); 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunThe dentry value can be NULL or an error value, in which case nothing will 239*4882a593Smuzhiyunbe removed. 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunOnce upon a time, debugfs users were required to remember the dentry 242*4882a593Smuzhiyunpointer for every debugfs file they created so that all files could be 243*4882a593Smuzhiyuncleaned up. We live in more civilized times now, though, and debugfs users 244*4882a593Smuzhiyuncan call:: 245*4882a593Smuzhiyun 246*4882a593Smuzhiyun void debugfs_remove_recursive(struct dentry *dentry); 247*4882a593Smuzhiyun 248*4882a593SmuzhiyunIf this function is passed a pointer for the dentry corresponding to the 249*4882a593Smuzhiyuntop-level directory, the entire hierarchy below that directory will be 250*4882a593Smuzhiyunremoved. 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun.. [1] http://lwn.net/Articles/309298/ 253