1. 程式人生 > >linux裝置驅動學習筆記--核心除錯方法之proc

linux裝置驅動學習筆記--核心除錯方法之proc

/proc 檔案系統是 GNU/Linux 特有的。它是一個虛擬的檔案系統,因此在該目錄中的所有檔案都不會消耗磁碟空間。通過它能夠非常簡便地瞭解系統資訊,尤其是其中的大部分檔案是人類可閱讀的(不過還是需要一些幫助)。許多程式實際上只是從 /proc 的檔案中收集資訊,然後按照它們自己的格式組織後顯示出來。有一些顯示程序資訊的程式(top、ps 等)就是這麼作的。/proc 還是瞭解您系統硬體的好去處。就象那些顯示程序資訊的程式一樣,不少程式只是提供了獲取 /proc 中資訊的介面。

最初開發 /proc 檔案系統是為了提供有關係統中程序的資訊。但是由於這個檔案系統非常有用並且使用簡單,因此核心中的很多元素也開始使用它來報告資訊,或啟用動態執行時配置。如在printk一章中說的在使用者態控制核心printk列印級別以及列印速率。

通過/proc檔案系統除錯核心模組非常簡單,核心已經實現好了框架,我們只需要實現一到兩個回撥函式即可。常用的方法有以下幾個:

1,建立proc入口檔案

struct proc_dir_entry *create_proc_entry(const char *name,
mode_t mode, struct proc_dir_entry *parent);

系統中為了方便還提供了一個建立只讀檔案的介面create_proc_read_entry,其實質還是呼叫了create_proc_entry函式,程式碼如下:

static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
	mode_t mode, struct proc_dir_entry *base, 
	read_proc_t *read_proc, void * data)
{
	struct proc_dir_entry *res=create_proc_entry(name,mode,base);
	if (res) {
		res->read_proc=read_proc;
		res->data=data;
	}
	return res;
}

2,刪除proc入口檔案

void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

注意:入口項不存在關聯的所有者,也即對proc檔案的使用並不會作用到模組的引用計數上,因此proc檔案正在被使用時呼叫了此刪除函式就會有問題。另一方面,如果模組解除安裝時不呼叫此函式刪除入口項,有可能下次插入模組時會在同一路徑建立一個同名檔案,是不是很吃驚?但是在proc檔案中就有可能發生,所以一定要在解除安裝模組時呼叫此方法。

3,proc檔案讀回撥函式

static int (*proc_read)(char *page, char **start, 
   off_t off, int count,  int *eof, void *data);

4,proc檔案寫回調函式

static int proc_write_foobar(struct file *file,  const char *buffer, 
    unsigned long count,  void *data);

5,將讀、寫回調函式與create_proc_entry返回的物件聯絡起來就OK了,核心程式碼中有一個很好的例子(見文章的最後面),這裡就不再自己寫了。

6,另外通過proc檔案我們也可以實現一個開關檔案,我們在使用者態只給proc檔案中寫入Y/N,0/1之類的值,在核心中通過判斷proc檔案的值來開啟或者關閉模組的某一功能,其實核心中有一個更簡單的實現--debugfs,以後再詳細分析。

寫到這裡其實有一點怪異的地方,對檔案系統稍微瞭解一些就會知道在linux中一切都是檔案,對檔案的操作都是需要實現一個叫file_operations的結構體,proc也是一個檔案系統,其下的也都是檔案,為什麼上面幾個步驟中卻沒有看到file_operations結構體的影子呢?

這個其實可以猜測一下,核心中應該是實現了一個預設的file_operations結構體,其讀/寫函式實現正好使用了我們上面說的讀、寫回調函式,我們通過核心程式碼來驗證一下這個猜測是否正確。

1,create_proc_entry函式返回了一個struct proc_dir_entry結構體,看實現可以發現有file_operations結構體指標變數

struct proc_dir_entry {
	......
	const struct file_operations *proc_fops;    <==檔案操作結構體
	struct proc_dir_entry *next, *parent, *subdir;
	void *data;
	read_proc_t *read_proc;                    <==讀回撥
	write_proc_t *write_proc;                  <==寫回調
	......
};

2,create_proc_entry函式是先通過__proc_create建立一個proc_dir_entry結構體ent,然後通過呼叫proc_register來實現註冊
struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
					 struct proc_dir_entry *parent)
{
	struct proc_dir_entry *ent;
	nlink_t nlink;

	......

	ent = __proc_create(&parent, name, mode, nlink);
	if (ent) {
		if (proc_register(parent, ent) < 0) {
			kfree(ent);
			ent = NULL;
		}
	}
	return ent;
}
3,proc_register註冊時會判斷ent中的file_operations變數是否為空,如果為空則賦一個值
static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
{
	unsigned int i;
	struct proc_dir_entry *tmp;
	.......

	if (S_ISDIR(dp->mode)) {
		......
	} else if (S_ISLNK(dp->mode)) {
		......
	} else if (S_ISREG(dp->mode)) {
		if (dp->proc_fops == NULL)
			dp->proc_fops = &proc_file_operations;
		if (dp->proc_iops == NULL)
			dp->proc_iops = &proc_file_inode_operations;
	}
<span style="white-space:pre">	</span>......
	return 0;
}

4,系統中預設的file_operations變數實現了read和write方法,定義如下
static const struct file_operations proc_file_operations = {
	.llseek		= proc_file_lseek,
	.read		= proc_file_read,
	.write		= proc_file_write,
};
5,proc_file_read通過__proc_file_read實現讀操作
static ssize_t
proc_file_read(struct file *file, char __user *buf, size_t nbytes,
	       loff_t *ppos)
{
	......

	rv = __proc_file_read(file, buf, nbytes, ppos);

	pde_users_dec(pde);
	return rv;
}

6,__proc_file_read程式碼中呼叫了我們上面所說的read回撥函式來實現真正的讀操作,這就印證了我們前面的猜測是正確的。
static ssize_t
__proc_file_read(struct file *file, char __user *buf, size_t nbytes,
	       loff_t *ppos)
{
	......
	while ((nbytes > 0) && !eof) {
		count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);

		start = NULL;
		if (dp->read_proc) {
			
			n = dp->read_proc(page, &start, *ppos,
					  count, &eof, dp->data);
		} else
			break;

		......

		*ppos += start < page ? (unsigned long)start : n;
		nbytes -= n;
		buf += n;
		retval += n;
	}
	free_page((unsigned long) page);
	return retval;
}
注意:

在新核心系統中已經去掉了read_proc和write_proc兩個回撥函式,而是直接實現file_operations中的read和write的方法,個人覺得這樣挺好的,與其他檔案操作做到了一致,至少不至於看實現步驟總覺得怪怪的。

procfs_example.c

/*
 * procfs_example.c: an example proc interface
 *
 * Copyright (C) 2001, Erik Mouw ([email protected])
 *
 * This file accompanies the procfs-guide in the Linux kernel
 * source. Its main use is to demonstrate the concepts and
 * functions described in the guide.
 *
 * This software has been developed while working on the LART
 * computing board (http://www.lartmaker.nl), which was sponsored
 * by the Delt University of Technology projects Mobile Multi-media
 * Communications and Ubiquitous Communications.
 *
 * This program is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/jiffies.h>
#include <asm/uaccess.h>


#define MODULE_VERS "1.0"
#define MODULE_NAME "procfs_example"

#define FOOBAR_LEN 8

struct fb_data_t {
	char name[FOOBAR_LEN + 1];
	char value[FOOBAR_LEN + 1];
};


static struct proc_dir_entry *example_dir, *foo_file,
	*bar_file, *jiffies_file, *symlink;


struct fb_data_t foo_data, bar_data;


static int proc_read_jiffies(char *page, char **start,
			     off_t off, int count,
			     int *eof, void *data)
{
	int len;

	len = sprintf(page, "jiffies = %ld\n",
                      jiffies);

	return len;
}


static int proc_read_foobar(char *page, char **start,
			    off_t off, int count, 
			    int *eof, void *data)
{
	int len;
	struct fb_data_t *fb_data = (struct fb_data_t *)data;

	/* DON'T DO THAT - buffer overruns are bad */
	len = sprintf(page, "%s = '%s'\n", 
		      fb_data->name, fb_data->value);

	return len;
}


static int proc_write_foobar(struct file *file,
			     const char *buffer,
			     unsigned long count, 
			     void *data)
{
	int len;
	struct fb_data_t *fb_data = (struct fb_data_t *)data;

	if(count > FOOBAR_LEN)
		len = FOOBAR_LEN;
	else
		len = count;

	if(copy_from_user(fb_data->value, buffer, len))
		return -EFAULT;

	fb_data->value[len] = '\0';

	return len;
}


static int __init init_procfs_example(void)
{
	int rv = 0;

	/* create directory */
	example_dir = proc_mkdir(MODULE_NAME, NULL);
	if(example_dir == NULL) {
		rv = -ENOMEM;
		goto out;
	}
	/* create jiffies using convenience function */
	jiffies_file = create_proc_read_entry("jiffies", 
					      0444, example_dir, 
					      proc_read_jiffies,
					      NULL);
	if(jiffies_file == NULL) {
		rv  = -ENOMEM;
		goto no_jiffies;
	}

	/* create foo and bar files using same callback
	 * functions 
	 */
	foo_file = create_proc_entry("foo", 0644, example_dir);
	if(foo_file == NULL) {
		rv = -ENOMEM;
		goto no_foo;
	}

	strcpy(foo_data.name, "foo");
	strcpy(foo_data.value, "foo");
	foo_file->data = &foo_data;
	foo_file->read_proc = proc_read_foobar;
	foo_file->write_proc = proc_write_foobar;
		
	bar_file = create_proc_entry("bar", 0644, example_dir);
	if(bar_file == NULL) {
		rv = -ENOMEM;
		goto no_bar;
	}

	strcpy(bar_data.name, "bar");
	strcpy(bar_data.value, "bar");
	bar_file->data = &bar_data;
	bar_file->read_proc = proc_read_foobar;
	bar_file->write_proc = proc_write_foobar;
		
	/* create symlink */
	symlink = proc_symlink("jiffies_too", example_dir, 
			       "jiffies");
	if(symlink == NULL) {
		rv = -ENOMEM;
		goto no_symlink;
	}

	/* everything OK */
	printk(KERN_INFO "%s %s initialised\n",
	       MODULE_NAME, MODULE_VERS);
	return 0;

no_symlink:
	remove_proc_entry("bar", example_dir);
no_bar:
	remove_proc_entry("foo", example_dir);
no_foo:
	remove_proc_entry("jiffies", example_dir);
no_jiffies:			      
	remove_proc_entry(MODULE_NAME, NULL);
out:
	return rv;
}


static void __exit cleanup_procfs_example(void)
{
	remove_proc_entry("jiffies_too", example_dir);
	remove_proc_entry("bar", example_dir);
	remove_proc_entry("foo", example_dir);
	remove_proc_entry("jiffies", example_dir);
	remove_proc_entry(MODULE_NAME, NULL);

	printk(KERN_INFO "%s %s removed\n",
	       MODULE_NAME, MODULE_VERS);
}


module_init(init_procfs_example);
module_exit(cleanup_procfs_example);

MODULE_AUTHOR("Erik Mouw");
MODULE_DESCRIPTION("procfs examples");
MODULE_LICENSE("GPL");


相關推薦

linux裝置驅動學習筆記--核心除錯方法proc(補充seq_file)

上一節中的proc實現對於開關檔案,控制檔案,以及顯示很少資訊的檔案來說還是比較簡單的,但是對於需要輸出大量資訊像meminfo,或者結構化的資訊像cpuinfo等時就會顯得很笨拙,並且程式碼也很不好理解與維護。核心為了簡化這種proc檔案的實現提供了另外一種方案----s

linux裝置驅動學習筆記--核心除錯方法proc

/proc 檔案系統是 GNU/Linux 特有的。它是一個虛擬的檔案系統,因此在該目錄中的所有檔案都不會消耗磁碟空間。通過它能夠非常簡便地瞭解系統資訊,尤其是其中的大部分檔案是人類可閱讀的(不過還是需要一些幫助)。許多程式實際上只是從 /proc 的檔案中收集資訊,然後按

linux裝置驅動學習筆記--核心除錯方法printk

1,printk類似於使用者態的printf函式,但是比printf函式多了一個日誌級別,核心中最常見的日誌輸出都是通過呼叫printk來實現的,其列印級別有8種可能的記錄字串, 在標頭檔案 <linux/kernel.h> 裡定義: KERN_EMERG

linux I2C 裝置驅動學習筆記

一:I2C 概述           I2C是philips提出的外設匯流排.使用SCL時鐘線,SDA序列資料線這兩根訊號線就實現了裝置之間的資料互動,被非常廣泛地應用在CPU與EEPROM,實時鐘,小型LCD等裝置通訊中。  二:在linux下的驅動思路     linu

嵌入式Linux裝置驅動開發筆記(一)

一、Linux裝置的分類 字元裝置、塊裝置、網路裝置,三種裝置之間的區別是資料的互動模式,分別為: 位元組流、資料塊、資料包。 二、VFS核心結構體 VFS核心結構體定義在”linux/fs.h”標頭檔案中。 1、struct inode結構體 記

linux裝置驅動學習(6) 高階字元驅動學習--阻塞型I/0

提出問題:若驅動程式無法立即滿足請求,該如何響應? 比如:當資料不可用時呼叫read,或是在緩衝區已滿時,呼叫write 解決問題:驅動程式應該(預設)該阻塞程序,將其置入休眠狀態直到請求可繼續。 休眠: 當一個程序被置入休眠時,它會被標記為一種特殊狀態並從排程器執行佇列

宋寶華:Linux裝置驅動框架裡的設計模式——模板方法(Template Method)

本文系轉載,著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。 作者: 宋寶華 來源: 微信公眾號linux閱碼場(id: linuxdev) 前言 《設計模式》這本經典的書裡面定義了20多種設計模式,雖然都是面向物件的,似乎需要C++、Java這樣的語言才能實現,但是根據筆者前面反覆

手把手教你寫Linux裝置驅動---定時器(一)(基於友善臂4412開發板)

這個專題我們來說下Linux中的定時器。在Linux核心中,有這樣的一個定時器,叫做核心定時器,核心定時器用於控制某個函式,也就是定時器將要處理的函式在未來的某個特定的時間內執行。核心定時器註冊的處理函

linux裝置驅動開發》,基於最新的linux 4.0核心-----筆記

第二章 Linux 的核心結構及構建 ---->這一章是自己總結的 1、核心結構(主要是下面這幾個部分) 系統呼叫介面<–>System call interface 程序管理<------>Process manag

Linux裝置驅動開發學習筆記

2016.6.25 這部門主要是之前學習linux裝置驅動開發時候的一些筆記,主要學習的參考書是《Linux裝置驅動開發詳解第2版》 書連結:http://note.youdao.com/noteshare?id=bbf134da309035b2093c5abcd5c7c8ac&

深入linux裝置驅動程式核心機制(第九章) 讀書筆記

第9章 linux裝置驅動模型       本文歡迎轉載, 請標明出處        本文出處: http://blog.csdn.net/dyron 9.1 sysfs檔案系統          sysfs檔案系統可以取代ioctl的功能.     sysfs檔案系統

核心程式除錯手段 >>Linux裝置驅動程式

文章目錄 [0x100]常用核心除錯方式 [0x110]核心的DEBUG選項 [0x120]核心列印函式 >>printk [0x121]預設規則 [0x122]終端列印日誌級別配置 [0x12

Linux裝置驅動第一天學習筆記(如何將系統在開發板上執行起來、驅動開發基本步驟)

如何將系統在開發板上執行起來? 4.0 交叉編譯器的獲取?廠家提供 網上下載(廠家確認) 4.1 uboot進行操作? 1,解壓廠家原始碼 2,進入原始碼 3,make distclean 徹底刪除原始碼的目標、臨時檔案 4,make xxx_c

Arm+Linux核心驅動學習筆記

韋東山老師幫我們把框架搭建起來了,我們先來看一下: 框架: app:      open,read,write "1.txt" ---------------------------------------------  檔案的讀寫 檔案系統: vfat, ext2,

Linux裝置驅動程式學習(7)-核心的資料型別

由於前面的學習中有用到 第十一章 核心資料結構型別 的知識,所以我先看了。要點如下: 將linux 移植到新的體系結構時,開發者遇到的若干問題都與不正確的資料型別有關。堅持使用嚴格的資料型別和使用 -Wall -Wstrict-prototypes 進行編譯可能避免大部分

linux裝置驅動第四篇:從如何定位oops的程式碼行談驅動除錯方法

上一篇我們大概聊瞭如何寫一個簡單的字元裝置驅動,我們不是神,寫程式碼肯定會出現問題,我們需要在編寫程式碼的過程中不斷除錯。在普通的c應用程式中,我們經常使用printf來輸出資訊,或者使用gdb來除錯程式,那麼驅動程式如何除錯呢?我們知道在除錯程式時經常遇到的問題就是野指標

linux裝置驅動:從如何定位oops的程式碼行談驅動除錯方法

在普通的c應用程式中,我們經常使用printf來輸出資訊,或者使用gdb來除錯程式,那麼驅動程式如何除錯呢?我們知道在除錯程式時經常遇到的問題就是野指標或者陣列越界帶來的問題,在應用程式中執行這種程式就會報segmentation fault的錯誤,而由於驅動程

Linux裝置驅動學習筆記(概述)

由於在下能力相當有限,有不當之處,還望批評指正^_^一、概述在核心中,匯流排/裝置/驅動模型實現了對匯流排/裝置/驅動的管理。涉及的概念有struct bus_type(匯流排型別)。核心並不關心每種匯流排的實現細節,也未預先定義一共有哪些具體的匯流排型別。因此,開發者甚至可

Linux裝置驅動程式學習筆記7--時間、延遲及延緩操作

#include <linux/timer.h>struct timer_list {    struct list_head entry;    unsigned long expires;/*期望定時器執行的絕對 jiffies 值,不是一個 jiffies_64 值,因為定時器不被期望在將來

Linux運維學習筆記之一:運維的原則和學習方法

linux 運維 筆記 一直在用Linux,但從未系統學習過,從1月1日開始學習到7月16日結束,近七個月學習,讓自已對Linux有了新的認識,老男孩老師的課真的不錯,實戰性很強。由於只能中午和晚上10點以後才有時間,所以所有的實驗是在不同電腦上完成的,文中IP可能有點問題,但應該不會影響實驗。同時,為了保證