Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of DebugFS in the Linux kernel

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "what is the use of DebugFS in the Linux kernel", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the use of DebugFS in the Linux kernel?"

DebugFS, as its name implies, is a virtual file system for kernel debugging, where kernel developers exchange data with user space through debugfs. Similar virtual file systems, such as procfs and sysfs, are not actually stored on the hard disk, but are not set up until the Linux kernel is running.

In general, the most commonly used means of kernel debugging is printk. But printk is not easy to use in all cases, for example, there may be too much printed data, and the data we really care about is not so clear in a large number of outputs. Or we may need to modify some kernel variables while debugging, in which case printk is powerless, and if recompiling the kernel or driver in order to change a value is too inefficient, we need a temporary file system that can map the data we need to care about to user space. In the past, procfs could do this, and so could the newly introduced sysfs in the 2.6 era, but whether it is procfs or sysfs, using them to implement some debug requirements seems to deviate from the original intention of their creation. Procfs, for example, is designed to reflect the state information of the process, while sysfs is mainly used in the Linux device model. The interfaces of both procfs and sysfs should remain relatively stable because user-mode programs are likely to rely on them. Of course, if we only temporarily borrow procfs or sysfs for debug, it's okay to delete the relevant debug code before the code is released. However, if the relevant debugging excuses are going to exist in the kernel for a long time, they are not suitable for procfs and sysfs. Therefore, debugfs arises at the historic moment.

By default, debugfs is mounted under the directory / sys/kernel/debug. If there is no automatic mount in your distribution, you can do it manually with the following command:

# mount-t debugfs none / your/debugfs/dir

The Linux kernel provides a very concise API for debugfs, and the next part of this article will take a real example, where sample code can be downloaded.

This implementation establishes the following directory structure in debugfs:

Among them, a corresponds to a U8 type variable in the module, b and c under subdir are an array of characters in the corresponding module, but they are implemented in different ways.

In module_init, we first need to create the root directory mydebug:

My_debugfs_root = debugfs_create_dir ("mydebug", NULL)

The * parameter is the name of the directory, and the second parameter is used to specify the parent directory of this directory. If it is NULL, it means that it is placed in the root directory of debugfs.

Subdirectories are also implemented in debugfs_create_dir:

Sub_dir = debugfs_create_dir ("subdir", my_debugfs_root)

The code to create file an is very simple:

Debugfs_create_u8 ("a", 0644, my_debugfs_root, & a)

This means that the file name is "a", the file attribute is 0644, the parent directory is the "mydebug" created above, and the corresponding variable is an in the module.

The Linux kernel also provides some other API for creating debugfs files, please refer to the appendix to this article.

B is a character array of 32-bytes. In debugfs, the array can be implemented in blob wrapper.

Char hello [32] = "Hello world!\ n"; struct debugfs_blob_wrapper b; b.data = (void *) hello; b.size = strlen (hello) + 1; debugfs_create_blob ("b", 0644, my_debugfs_root, & b)

It is important to note that the data defined by blob wrapper can only be read-only. In this example, although we set the permission of file b to 0644, the file is actually read-only, and if you try to overwrite the file, the system will prompt for an error.

If you need to write to the kernel array, blob wrapper will not be able to meet the requirements, we can only define the file operation to achieve. In this implementation, you can refer to the implementation of file c. C and b correspond to the same character array in the module, except that b is read-only, while c reads and writes simultaneously through custom file operations.

Static int c_open (struct inode * inode, struct file * filp) {filp- > private_data = inode- > iPreventive; return 0;} static ssize_t c_read (struct file * filp, char _ user * buffer, size_t count, loff_t * ppos) {if (* ppos > = 32) return 0; if (* ppos + count > 32) count = 32-* ppos If (copy_to_user (buffer, hello + * ppos, count) return-EFAULT; * ppos + = count; return count;} static ssize_t c_write (struct file * filp, const char _ user * buffer, size_t count, loff_t * ppos) {if (* ppos > = 32) return 0; if (* ppos + count > 32) count = 32-* ppos If (copy_from_user (hello + * ppos, buffer, count) return-EFAULT; * ppos + = count; return count;} struct file_operations c_fops = {.owner = THIS_MODULE, .open = c_open, .read = c_read, .write = c_write,}; debugfs_create_file ("c", 0644, sub_dir, NULL, & c_fops)

Note: in the code, c_open is of no use, because c_read and c_write refer directly to the global variable hello. Here, we can also write it another way, using filp- > private_data in the read/write function to reference the character array hello.

At this point, three files and subdirectories have been created. In module_exit, we need to remember to release the created data.

Debugfs_remove_recursive (my_debugfs_root)

Debugfs_remove_recursive can help us gradually remove each allocation.

At this point, I believe you have a deeper understanding of "what is the use of DebugFS in the Linux kernel?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report