Linux Kernel – Adding Debugfs support to a Linux Kernel Module

The below code creates a directory in “/sys/kernel/debug” and a value is written to file and read from file. The code and the “Makefile” for compiling the code are provided below: 

In the above code example, the file created is “value” inside the folder “/sys/kernel/debug/debug_dir_example“. When “debugfs_create_file” API is invoked, the “data” parameter (accessed in the debugfs_set/debugfs_get calls) is set to NULL. Hence, modify and read from a simple global parameter (u32 value) is what is attempted in the code. 

Makefile:

The “dmesg” output and the value written and read is shown below. Note that as discussed in the previous article, debug folder is accessible in root mode. Hence, “sudo -i” is used in the Ubuntu OS terminal below to enter into root mode before accessing the “sys/kernel/debug” folder. 

In the output above, when the file is opened via “vi command“, the file will show 42. The “dmesg” output indicates the same.

Allocating Memory in the Linux Kernel Module – Kernel APIs

Comments

  1. Pingback: Linux Kernel – Debug File System | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *