Linux之debugfs介紹__Linux

來源:互聯網
上載者:User
-----------------------------------------------------------------------
本文系本站原創,歡迎轉載!
轉載請註明出處:http://blog.csdn.net/android_huber
交流郵箱:dp.shao@gmail.com
-----------------------------------------------------------------------
Linux之debugfs介紹

Debugfs是一種用於核心調試的虛擬檔案系統,在核心源碼中經常可以看到它的使用,今天我來大概介紹一下它的使用。

如果你對debugfs不熟悉,那麼也許你會對sysfs比較熟悉,對於調試方面,其實兩個檔案系統還是挺類似的,但是sysfs的引入核心主要是用於驅動模型的。所以我們在平時調試的時候應該盡量避免使用sysfs,而使用debugfs。

好了,下面我們來介紹一些debugfs的使用,要使用debugfs首先我們要設定一下配置選項CONFIG_DEBUG_FS,可以在config檔案中設定CONFIG_DEBUG_FS=y,也可以通過menuconfig來設定,如圖:

Kernelhacking --->            [*]Debug Filesystem

驅動中使用debugfs需要包含標頭檔<linux/debugfs.h>,為了在使用者態下使用debugfs,必須把它mount到一個目錄下,我們可以把它放在mnt目錄下。

使用如下命令:

mount-t debugfs none /mnt

然後進入 /mnt後就可以看到我們在系統中建立的這些檔案。

下面我們開始說一下如何在驅動中使用debugfs.

首先我們需要建立一個自己的目錄,利用如下函數:

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

name 就是建立的目錄的名字, parent 是該目錄的父目錄,如果是 NULL 的話,則所建立的目錄就在 debugfs 的根目錄,具體使用如下:

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

這樣就會在debugfs的根目錄下建立一個binder的目錄,有了目錄還需要有可供讀寫的檔案吧,下邊就是另一個重要的函數,檔案的建立:

struct dentry *debugfs_create_file(const char *name, mode_t mode,struct dentry *parent, void *data,const struct file_operations *fops)

如其函數名,這個函數的作用就是在parent這個目錄下建立一個名為name的檔案,mode是這個檔案讀寫權限,data是傳入的參數,fops就比較重要了,為我們的檔案提供實際的讀寫操作。

在binder驅動中建立了如下檔案

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);

如上圖所示,在binder目錄下建立了proc/state/stats/transactions/transaction_log/failed_transaction_log這些檔案。

在binder中這些檔案的fops全部用一個宏來完成了

#define BINDER_DEBUG_ENTRY(name) \static int binder_##name##_open(struct inode *inode, struct file *file) \{\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, \}

可以看到binder沒有實現write操作。read則全部使用seq_file的操作,seq_file是2.4.15以後版本的核心中出現的新功能,使得核心輸出大檔案資訊。這邊我們就不去將seq了,我們放在另外的一篇文章裡面去講。

debugfs除了實現這個檔案對函數的功能,還提供了一些API用來針對變數的操作:

struct dentry *debugfs_create_u8(const char *name, mode_t mode, struct dentry *parent, u8 *value);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);

這樣就可以在使用者態去調試核心變數value了。

當核心卸載時,debugfs並不會自動的去清除我們建立的這些檔案,對於建立的每一個檔案我們都需要調用如下函數介面去清除:

void debugfs_remove(struct dentry *dentry);


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.