debugfs.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. =======
  4. DebugFS
  5. =======
  6. Copyright |copy| 2009 Jonathan Corbet <corbet@lwn.net>
  7. Debugfs exists as a simple way for kernel developers to make information
  8. available to user space. Unlike /proc, which is only meant for information
  9. about a process, or sysfs, which has strict one-value-per-file rules,
  10. debugfs has no rules at all. Developers can put any information they want
  11. there. The debugfs filesystem is also intended to not serve as a stable
  12. ABI to user space; in theory, there are no stability constraints placed on
  13. files exported there. The real world is not always so simple, though [1]_;
  14. even debugfs interfaces are best designed with the idea that they will need
  15. to be maintained forever.
  16. Debugfs is typically mounted with a command like::
  17. mount -t debugfs none /sys/kernel/debug
  18. (Or an equivalent /etc/fstab line).
  19. The debugfs root directory is accessible only to the root user by
  20. default. To change access to the tree the "uid", "gid" and "mode" mount
  21. options can be used.
  22. Note that the debugfs API is exported GPL-only to modules.
  23. Code using debugfs should include <linux/debugfs.h>. Then, the first order
  24. of business will be to create at least one directory to hold a set of
  25. debugfs files::
  26. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
  27. This call, if successful, will make a directory called name underneath the
  28. indicated parent directory. If parent is NULL, the directory will be
  29. created in the debugfs root. On success, the return value is a struct
  30. dentry pointer which can be used to create files in the directory (and to
  31. clean it up at the end). An ERR_PTR(-ERROR) return value indicates that
  32. something went wrong. If ERR_PTR(-ENODEV) is returned, that is an
  33. indication that the kernel has been built without debugfs support and none
  34. of the functions described below will work.
  35. The most general way to create a file within a debugfs directory is with::
  36. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  37. struct dentry *parent, void *data,
  38. const struct file_operations *fops);
  39. Here, name is the name of the file to create, mode describes the access
  40. permissions the file should have, parent indicates the directory which
  41. should hold the file, data will be stored in the i_private field of the
  42. resulting inode structure, and fops is a set of file operations which
  43. implement the file's behavior. At a minimum, the read() and/or write()
  44. operations should be provided; others can be included as needed. Again,
  45. the return value will be a dentry pointer to the created file,
  46. ERR_PTR(-ERROR) on error, or ERR_PTR(-ENODEV) if debugfs support is
  47. missing.
  48. Create a file with an initial size, the following function can be used
  49. instead::
  50. void debugfs_create_file_size(const char *name, umode_t mode,
  51. struct dentry *parent, void *data,
  52. const struct file_operations *fops,
  53. loff_t file_size);
  54. file_size is the initial file size. The other parameters are the same
  55. as the function debugfs_create_file.
  56. In a number of cases, the creation of a set of file operations is not
  57. actually necessary; the debugfs code provides a number of helper functions
  58. for simple situations. Files containing a single integer value can be
  59. created with any of::
  60. void debugfs_create_u8(const char *name, umode_t mode,
  61. struct dentry *parent, u8 *value);
  62. void debugfs_create_u16(const char *name, umode_t mode,
  63. struct dentry *parent, u16 *value);
  64. void debugfs_create_u32(const char *name, umode_t mode,
  65. struct dentry *parent, u32 *value);
  66. void debugfs_create_u64(const char *name, umode_t mode,
  67. struct dentry *parent, u64 *value);
  68. These files support both reading and writing the given value; if a specific
  69. file should not be written to, simply set the mode bits accordingly. The
  70. values in these files are in decimal; if hexadecimal is more appropriate,
  71. the following functions can be used instead::
  72. void debugfs_create_x8(const char *name, umode_t mode,
  73. struct dentry *parent, u8 *value);
  74. void debugfs_create_x16(const char *name, umode_t mode,
  75. struct dentry *parent, u16 *value);
  76. void debugfs_create_x32(const char *name, umode_t mode,
  77. struct dentry *parent, u32 *value);
  78. void debugfs_create_x64(const char *name, umode_t mode,
  79. struct dentry *parent, u64 *value);
  80. These functions are useful as long as the developer knows the size of the
  81. value to be exported. Some types can have different widths on different
  82. architectures, though, complicating the situation somewhat. There are
  83. functions meant to help out in such special cases::
  84. void debugfs_create_size_t(const char *name, umode_t mode,
  85. struct dentry *parent, size_t *value);
  86. As might be expected, this function will create a debugfs file to represent
  87. a variable of type size_t.
  88. Similarly, there are helpers for variables of type unsigned long, in decimal
  89. and hexadecimal::
  90. struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
  91. struct dentry *parent,
  92. unsigned long *value);
  93. void debugfs_create_xul(const char *name, umode_t mode,
  94. struct dentry *parent, unsigned long *value);
  95. Boolean values can be placed in debugfs with::
  96. void debugfs_create_bool(const char *name, umode_t mode,
  97. struct dentry *parent, bool *value);
  98. A read on the resulting file will yield either Y (for non-zero values) or
  99. N, followed by a newline. If written to, it will accept either upper- or
  100. lower-case values, or 1 or 0. Any other input will be silently ignored.
  101. Also, atomic_t values can be placed in debugfs with::
  102. void debugfs_create_atomic_t(const char *name, umode_t mode,
  103. struct dentry *parent, atomic_t *value)
  104. A read of this file will get atomic_t values, and a write of this file
  105. will set atomic_t values.
  106. Another option is exporting a block of arbitrary binary data, with
  107. this structure and function::
  108. struct debugfs_blob_wrapper {
  109. void *data;
  110. unsigned long size;
  111. };
  112. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  113. struct dentry *parent,
  114. struct debugfs_blob_wrapper *blob);
  115. A read of this file will return the data pointed to by the
  116. debugfs_blob_wrapper structure. Some drivers use "blobs" as a simple way
  117. to return several lines of (static) formatted text output. This function
  118. can be used to export binary information, but there does not appear to be
  119. any code which does so in the mainline. Note that all files created with
  120. debugfs_create_blob() are read-only.
  121. If you want to dump a block of registers (something that happens quite
  122. often during development, even if little such code reaches mainline),
  123. debugfs offers two functions: one to make a registers-only file, and
  124. another to insert a register block in the middle of another sequential
  125. file::
  126. struct debugfs_reg32 {
  127. char *name;
  128. unsigned long offset;
  129. };
  130. struct debugfs_regset32 {
  131. const struct debugfs_reg32 *regs;
  132. int nregs;
  133. void __iomem *base;
  134. struct device *dev; /* Optional device for Runtime PM */
  135. };
  136. debugfs_create_regset32(const char *name, umode_t mode,
  137. struct dentry *parent,
  138. struct debugfs_regset32 *regset);
  139. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  140. int nregs, void __iomem *base, char *prefix);
  141. The "base" argument may be 0, but you may want to build the reg32 array
  142. using __stringify, and a number of register names (macros) are actually
  143. byte offsets over a base for the register block.
  144. If you want to dump a u32 array in debugfs, you can create a file with::
  145. struct debugfs_u32_array {
  146. u32 *array;
  147. u32 n_elements;
  148. };
  149. void debugfs_create_u32_array(const char *name, umode_t mode,
  150. struct dentry *parent,
  151. struct debugfs_u32_array *array);
  152. The "array" argument wraps a pointer to the array's data and the number
  153. of its elements. Note: Once array is created its size can not be changed.
  154. There is a helper function to create a device-related seq_file::
  155. void debugfs_create_devm_seqfile(struct device *dev,
  156. const char *name,
  157. struct dentry *parent,
  158. int (*read_fn)(struct seq_file *s,
  159. void *data));
  160. The "dev" argument is the device related to this debugfs file, and
  161. the "read_fn" is a function pointer which to be called to print the
  162. seq_file content.
  163. There are a couple of other directory-oriented helper functions::
  164. struct dentry *debugfs_rename(struct dentry *old_dir,
  165. struct dentry *old_dentry,
  166. struct dentry *new_dir,
  167. const char *new_name);
  168. struct dentry *debugfs_create_symlink(const char *name,
  169. struct dentry *parent,
  170. const char *target);
  171. A call to debugfs_rename() will give a new name to an existing debugfs
  172. file, possibly in a different directory. The new_name must not exist prior
  173. to the call; the return value is old_dentry with updated information.
  174. Symbolic links can be created with debugfs_create_symlink().
  175. There is one important thing that all debugfs users must take into account:
  176. there is no automatic cleanup of any directories created in debugfs. If a
  177. module is unloaded without explicitly removing debugfs entries, the result
  178. will be a lot of stale pointers and no end of highly antisocial behavior.
  179. So all debugfs users - at least those which can be built as modules - must
  180. be prepared to remove all files and directories they create there. A file
  181. can be removed with::
  182. void debugfs_remove(struct dentry *dentry);
  183. The dentry value can be NULL or an error value, in which case nothing will
  184. be removed.
  185. Once upon a time, debugfs users were required to remember the dentry
  186. pointer for every debugfs file they created so that all files could be
  187. cleaned up. We live in more civilized times now, though, and debugfs users
  188. can call::
  189. void debugfs_remove_recursive(struct dentry *dentry);
  190. If this function is passed a pointer for the dentry corresponding to the
  191. top-level directory, the entire hierarchy below that directory will be
  192. removed.
  193. .. [1] http://lwn.net/Articles/309298/