create_proc_read_entry

使用proc文件系统来读取内核中的信息非常有用。
实例:在开发I2C相关驱动时,在内核启动后用户空间想要知道I2C相关寄存器的值时,可以这样做:

1)在I2C驱动模块中定义实际调用的函数

static int read_i2cinfo_from_proc(char *buf, char **start, off_t offset, int count,
int *eof, void *data)
{
int len=0;
len +=sprintf(buf+len,”I2C reg is 0x%x\n”,reg);
return len;
}

2)在I2C模块(如I2C算法驱动模块)初始化时建立一个proc entry

create_proc_read_entry(“readi2cinfo”, 0, 0, read_i2cinfo_from_proc, NULL);
//readi2cinfo为/proc目录中的文件名字
//read_i2cinfo_from_proc是实际调用的函数,在1)中定义

3)在用户空间查看当前I2C寄存器的值

$cat /proc/readi2cinfo

/*
*By Neil Zhao (neilzhao at refreshsys.com)
*转载请注明出处:http://www.refreshsys.com
*/

转载于:https://www.cnblogs.com/ke-frank/archive/2011/03/16/1985822.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
``` /*pf.c*/ /*内核模块代码*/ /* 引入需要的头文件 */ #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/proc_fs.h> #include <linux/string.h> #include <asm/uaccess.h> /* 定义 proc 文件系统的对象 */ struct proc_dir_entry *proc_pf; struct proc_dir_entry *proc_pfcount; /* 定义全局变量,用于记录页面错误的次数 */ extern unsigned long volatile pfcount; /* 创建 proc 文件的函数 */ static inline struct proc_dir_entry *proc_pf_create(const char* name, mode_t mode, read_proc_t *get_info) { return create_proc_read_entry(name, mode, proc_pf, get_info, NULL); } /* 读取 proc 文件的函数 */ int get_pfcount(char *buffer, char **start, off_t offset, int length, int *peof,void *data) { int len = 0; len = sprintf(buffer, "%ld \n", pfcount); /* 使用 sprintf 函数将 pfcount 转换成字符串,存储到 buffer 中 */ return len; } /* 初始化模块的函数 */ static int pf_init(void) { proc_pf = proc_mkdir("pf", 0); /* 在 proc 文件系统中创建一个目录 pf */ proc_pf_create("pfcount", 0, get_pfcount); /* 在 pf 目录下创建一个文件 pfcount,并注册读取函数 get_pfcount */ return 0; } /* 卸载模块的函数 */ static void pf_exit(void) { remove_proc_entry("pfcount", proc_pf); /* 删除 pf 目录下的文件 pfcount */ remove_proc_entry("pf", 0); /* 删除 pf 目录 */ } /* 使用 module_init 和 module_exit 宏指定初始化和卸载函数 */ module_init(pf_init); module_exit(pf_exit); /* 使用 MODULE_LICENSE 和 MODULE_AUTHOR 宏指定模块的许可证和作者信息 */ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Aron.t.wang"); ``` 以上就是对 pf.c 内核模块代码的详细注释。此模块主要是创建了一个 proc 文件系统的目录和一个 proc 文件,用于记录页面错误(page fault)的次数,以方便系统调试和性能分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值