Linux调试中使用的proc和sys中的接口

来源:互联网 发布:淘宝代购上传身份证 编辑:程序博客网 时间:2024/05/22 03:04
在调试的过程中我们通常需要去不停的修改寄存器的值来调试效果,在现在发现有两个手段,分别利用的proc和sys
proc--|
      |-----read_proc
      |-----write_proc
      
sys---|
      |-----show
      |-----store


proc的方法不依赖与kobject,sys中show和store则依赖于kobject。      
基本的使用如下:
----------------------------------------------------------------------
--------------------------  start proc   ----------------------------
----------------------------------------------------------------------


struct proc_dir_entry *prEntry;    prEntry = create_proc_entry("driver/proc_func", 0, NULL);     if (prEntry) {        prEntry->read_proc = read_proc_func;         prEntry->write_proc = write_proc_func;     }    else {        printk("add /proc/driver/camsensor entry fail \n");      }


对于read_proc和write_proc的定义则参考proc_fs.h中的定义即可以实现
/* * This is not completely implemented yet. The idea is to * create an in-memory tree (like the actual /proc filesystem * tree) of these proc_dir_entries, so that we can dynamically * add new files to /proc. * * The "next" pointer creates a linked list of one /proc directory, * while parent/subdir create the directory structure (every * /proc file has a parent, but "subdir" is NULL for all * non-directory entries). */typedefint (read_proc_t)(char *page, char **start, off_t off,  int count, int *eof, void *data);typedefint (write_proc_t)(struct file *file, const char __user *buffer,   unsigned long count, void *data);

----------------------------------------------------------------------
--------------------------   end  proc   ---------------------------
----------------------------------------------------------------------






相对的利用sys中实现接口调试则如下实现,前提是有device设备哦,亲。
---------------------------------------------------------------------
--------------------------   start  sys   --------------------------
---------------------------------------------------------------------


static struct device_attribute test1_attr = {.attr = {.name = "test1",.mode = 0644,.owner = THIS_MODULE},.show = test1_show_func,.store = test1_store_func,};static struct device_attribute *test_attributes[] = {&test1_attr,&test2_attr,&test3_attr,};for (i = 0; i < ARRAY_SIZE(*test_attributes); i++) {ret = device_create_file(&dev,*test_attributes[i]);if (ret) {printk("failed: sysfs file %s\n",*test_attributes[i]->attr.name);goto failed_unregister_dev_file;}}

关于show和store的函数的定义则如下
/* interface for exporting device attributes */struct device_attribute {struct attributeattr;ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);};

---------------------------------------------------------------------
--------------------------    end   sys   --------------------------
----------------------------------------------------------------------


通过这两种方式可以在调试LCM或者是Camera的时候不必要重新编译了,对调试效果帮助很大。