debugfs.txt 8.1 KB

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