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


在调试的过程中我们通常需要去不停的修改寄存器的值来调试效果,在现在发现有两个手段,分别利用的proc和sys
proc--|
      |-----read_proc
      |-----write_proc
      
sys---|
      |-----show
      |-----store


proc的方法不依赖与kobject,sys中show和store则依赖于kobject。

基本的使用如下:

--------------------------  start proc   ----------------------------

  1. struct proc_dir_entry *prEntry;  
  2.   
  3.   
  4.    prEntry = create_proc_entry("driver/proc_func", 0, NULL);   
  5.    if (prEntry) {  
  6.        prEntry->read_proc = read_proc_func;   
  7.        prEntry->write_proc = write_proc_func;   
  8.    }  
  9.    else {  
  10.        printk("add /proc/driver/camsensor entry fail \n");    
  11.    }  
对于read_proc和write_proc的定义则参考proc_fs.h中的定义即可以实现
  1. /* 
  2.  * This is not completely implemented yet. The idea is to 
  3.  * create an in-memory tree (like the actual /proc filesystem 
  4.  * tree) of these proc_dir_entries, so that we can dynamically 
  5.  * add new files to /proc. 
  6.  * 
  7.  * The "next" pointer creates a linked list of one /proc directory, 
  8.  * while parent/subdir create the directory structure (every 
  9.  * /proc file has a parent, but "subdir" is NULL for all 
  10.  * non-directory entries). 
  11.  */  
  12.   
  13.   
  14. typedef int (read_proc_t)(char *page, char **start, off_t off,  
  15.               int count, int *eof, void *data);  
  16. typedef int (write_proc_t)(struct file *file, const char __user *buffer,  
  17.                unsigned long count, void *data);  
--------------------------   end  proc   ---------------------------

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

--------------------------   start  sys   --------------------------
  1. static struct device_attribute test1_attr = {  
  2.     .attr = {  
  3.         .name = "test1",  
  4.         .mode = 0644,  
  5.         .owner = THIS_MODULE  
  6.     },  
  7.     .show = test1_show_func,  
  8.     .store = test1_store_func,  
  9. };  
  10.   
  11.   
  12. static struct device_attribute *test_attributes[] = {  
  13.     &test1_attr,  
  14.     &test2_attr,  
  15.     &test3_attr,  
  16. };  
  17.   
  18.   
  19. for (i = 0; i < ARRAY_SIZE(*test_attributes); i++) {  
  20.     ret = device_create_file(&dev,  
  21.                     *test_attributes[i]);  
  22.     if (ret) {  
  23.         printk("failed: sysfs file %s\n",*test_attributes[i]->attr.name);  
  24.         goto failed_unregister_dev_file;  
  25.     }  
  26. }  
关于show和store的函数的定义则如下
  1. /* interface for exporting device attributes */  
  2. struct device_attribute {  
  3.     struct attribute    attr;  
  4.     ssize_t (*show)(struct device *dev, struct device_attribute *attr,  
  5.             char *buf);  
  6.     ssize_t (*store)(struct device *dev, struct device_attribute *attr,  
  7.              const char *buf, size_t count);  
  8. };  

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

相关内容