Introduction of Linux Debugfs __linux

Source: Internet
Author: User
-----------------------------------------------------------------------
This article is the original site, welcome to reprint!
Reprint please indicate the source: Http://blog.csdn.net/android_huber
Exchange Email: dp.shao@gmail.com
-----------------------------------------------------------------------
introduction of Linux Debugfs

Debugfs is a kernel debugging of the virtual file system, in the kernel source code can often see its use, today I would like to introduce its use.

If you are unfamiliar with Debugfs, then perhaps you will be familiar with the SYSFS, for debugging, in fact, two file system is quite similar, but the introduction of the SYSFS kernel is mainly used to drive the model. So we should try to avoid using SYSFS when we are in normal debugging, and use Debugfs.

OK, let's introduce some DEBUGFS use, to use Debugfs first we have to set the configuration option Config_debug_fs, you can set config_debug_fs=y in config file, You can also set it by Menuconfig, as shown in figure:

kernelhacking--->

            [*]debug filesystem

Drivers need to include header file <linux/debugfs.h> in the driver, in order to use DEBUGFS in user state, it must be debugfs to a directory, we can put it in the MNT directory.

Use the following command:

Mount-t Debugfs none/mnt

Then you can see the files we created in the system after entering the/mnt.

Let's start by saying how to use Debugfs in a driver.

First we need to create a directory of our own, using the following functions:

struct Dentry *debugfs_create_dir (const char *name, struct dentry);

Name is the directory that is created, parent is the directory, and if it is NULL, the directory created is in the root directory of the Debugfs, using the following:

static struct dentry *binder_debugfs_dir_entry_root;
binder_debugfs_dir_entry_root= debugfs_create_dir ("binder", NULL);

This will create a binder directory under the root directory of the Debugfs, with a directory to read and write files, the following is another important function, the creation of the file:

struct Dentry *debugfs_create_file (const char *name, mode_t mode,
struct dentry, void *parent,
const *DATA File_operations *fops)

As its function name, this function is in the directory of parent to create a file named Name, mode is the file read and write permissions, data is the incoming parameters, FoPs is more important for our files to provide the actual reading and writing operations.

The following files were created in the binder drive

Debugfs_create_file ("state",
S_irugo,
binder_debugfs_dir_entry_root,
NULL,
&binder_state_ FoPs);

Debugfs_create_file ("stats",
S_irugo,
binder_debugfs_dir_entry_root,
NULL,
&binder_stats_ FoPs);

Debugfs_create_file ("Transactions",
S_irugo,
binder_debugfs_dir_entry_root,
NULL,
& Binder_transactions_fops);

Debugfs_create_file ("Transaction_log",
S_irugo,
binder_debugfs_dir_entry_root,
&binder_ Transaction_log,
&binder_transaction_log_fops);

Debugfs_create_file ("Failed_transaction_log",
S_irugo,
binder_debugfs_dir_entry_root,
& binder_transaction_log_failed,
&binder_transaction_log_fops);

As shown in the figure above, the Proc/state/stats/transactions/transaction_log/failed_transaction_log files are created in the Binder directory.

In binder, the fops of these files is all done with a single macro.

#define BINDER_DEBUG_ENTRY (name) \
static int binder_# #name # #_open (struct inode *inode, struct file *file) \
{\< C2/>return single_open (file, binder_# #name # #_show, inode->i_private); \
}\
\
static const struct file_operations binder_# #name # #_fops = {\
. owner= this_module, \
. open= binder_# #name # #_open, \
read= seq_read, \
. llseek= seq_lseek, \
. release= single_release, \
}

You can see that binder did not implement the write operation. Read uses all seq_file operations, and Seq_file is a new feature that appears in the 2.4.15 later versions of the kernel, making the kernel output large file information. This way we will not go to the SEQ, we put in another article inside to speak.

In addition to implementing the functions of this file on functions, Debugfs also provides some of the APIs used to manipulate variables:

struct Dentry *debugfs_create_u8 (const char *name, mode_t mode, struct dentry, *parent U8);
struct Dentry *debugfs_create_u16 (const char *name, mode_t mode, Structdentry *parent, U16 *value);
struct Dentry *debugfs_create_u32 (const char *name, mode_t mode, Structdentry *parent, u32 *value);
struct Dentry *debugfs_create_bool (const char *name, mode_t mode, Structdentry *parent, u32 *value);

This allows you to debug kernel variable value in user state.

When the kernel unloads, Debugfs does not automatically erase the files we create, and for each file we create we need to call the following function interface to clear:

void Debugfs_remove (struct dentry *dentry);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.