debugfs.c 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Adapted from: https://github.com/chadversary/debugfs-tutorial/blob/47b3cf7ca47208c61ccb51b27aac6f9f932bfe0b/example1/debugfs_example1.c
  3. Usage:
  4. /debugfs.sh
  5. Requires `CONFIG_DEBUG_FS=y`.
  6. Only the more basic fops can be implemented in debugfs, e.g. mmap is never called:
  7. - https://patchwork.kernel.org/patch/9252557/
  8. - https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <uapi/linux/stat.h> /* S_IRUSR */
  14. static struct dentry *dir;
  15. static u32 value = 42;
  16. static int myinit(void)
  17. {
  18. struct dentry *file;
  19. dir = debugfs_create_dir("lkmc_debugfs", 0);
  20. if (!dir) {
  21. printk(KERN_ALERT "debugfs_create_dir failed");
  22. return -1;
  23. }
  24. file = debugfs_create_u32("myfile", S_IRUSR, dir, &value);
  25. if (!file) {
  26. printk(KERN_ALERT "debugfs_create_u32 failed");
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. static void myexit(void)
  32. {
  33. debugfs_remove_recursive(dir);
  34. }
  35. module_init(myinit)
  36. module_exit(myexit)
  37. MODULE_LICENSE("GPL");