DebugFS is a synthetic filesystem in the kernel. It resides at
/sys/kernel/debug
and allows userspace processes to interact with variables
and functions in the kernel. Unlike sysfs, the information provided by debugfs
is intended for kernel debugging only.
This tutorial is hands-on with kernel code. For each example:
- Read the README,
- Open the source file,
- Follow the comments.
Author: Lina Versace <[email protected]>
The kernel module debugfs_example1 creates the below files in debugfs:
/sys/kernel/debug/example1
/sys/kernel/debug/example1/hello
Reading/writing to file hello actually reads/writes to a variable hello in the module.
$ cd example1
$ make
$ sudo insmod debugfs_example1.ko
$ cd /sys/kernel/debug/example1
$ ls -l
-rw-rw-rw- hello
$ cat hello
0
$ echo 42 > hello
$ cat hello
42
$ echo 3 > hello
$ cat hello
3
$ sudo rmmod debugfs_example1
The kernel module debugfs_example2 creates the below files in debugfs:
/sys/kernel/debug/example2
/sys/kernel/debug/example2/add
/sys/kernel/debug/example2/sum
File sum points to the variable sum in the module. File add is proxy to the function add_write_op which increments the sum variable. Appending integers to file add will increase sum.
$ cd example2
$ make
$ sudo insmod debugfs_example2.ko
$ cd /sys/kernel/debug/example2
$ ls -l
--w--w--w- add
-r--r--r-- sum
$ cat sum
0
$ echo 17 >> add
$ cat sum
17
$ echo 2 >> add
$ cat sum
19
$ sudo rmmod debugfs_example2
SPDX-License-Identifier: GPL-2.0 OR MIT
Copyright 2010 Lina Versace [email protected]
Indiviual licenses files can be found in the licenses folder.