自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux內(nèi)核調(diào)試技術之自構proc

系統(tǒng) Linux
在內(nèi)核中使用printk可以講調(diào)試信息保存在log_buf緩沖區(qū)中,可以使用命令 #cat /proc/kmsg 將緩沖區(qū)的數(shù)區(qū)的數(shù)數(shù)據(jù)打印出來,今天我們就來研究一下,自己寫kmsg這個文件,我們?nèi)∶凶?mymsg。

1、簡介

在內(nèi)核中使用printk可以講調(diào)試信息保存在log_buf緩沖區(qū)中,可以使用命令 #cat /proc/kmsg 將緩沖區(qū)的數(shù)區(qū)的數(shù)數(shù)據(jù)打印出來,今天我們就來研究一下,自己寫kmsg這個文件,我們?nèi)∶凶?mymsg。

2、查看內(nèi)核中 /proc/kmsg怎么寫的!

在Proc_misc.c (fs\proc) 文件中:

  1. void __init proc_misc_init(void){ 
  2.     ......................... 
  3.         struct proc_dir_entry *entry; 
  4.         //這里創(chuàng)建了一個proc入口kmsg 
  5.         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root); 
  6.         if (entry)       /*構造一個proc_fops結(jié)構*/ 
  7.        entry->proc_fops = &proc_kmsg_operations;  ......................... }  

在Kmsg.c (fs\proc) 文件中:

  1. const struct file_operations proc_kmsg_operations = { 
  2.     .read        = kmsg_read, 
  3.     .poll        = kmsg_poll, 
  4.     .open        = kmsg_open, 
  5.     .release    = kmsg_release,};  

在用戶空間中使用 cat /proc/kmsg的時候,會調(diào)用kmsg_open,在調(diào)用kmsg_read函數(shù),讀取log_buf中的數(shù)據(jù),拷貝到用戶空間顯示。

3、在寫之前,我們需要來學習一下循環(huán)隊列

環(huán)形隊列是在實際編程極為有用的數(shù)據(jù)結(jié)構,它有如下特點:

  • 它是一個首尾相連的FIFO的數(shù)據(jù)結(jié)構,采用數(shù)組的線性空間,數(shù)據(jù)組織簡單,能很快知道隊列是否滿為空。能以很快速度的來存取數(shù)據(jù)。
  • 因為有簡單高效的原因,甚至在硬件都實現(xiàn)了環(huán)形隊列。
  • 環(huán)形隊列廣泛用于網(wǎng)絡數(shù)據(jù)收發(fā),和不同程序間數(shù)據(jù)交換(比如內(nèi)核與應用程序大量交換數(shù)據(jù),從硬件接收大量數(shù)據(jù))均使用了環(huán)形隊列。

3.1.環(huán)形隊列實現(xiàn)原理

內(nèi)存上沒有環(huán)形的結(jié)構,因此環(huán)形隊列實上是數(shù)組的線性空間來實現(xiàn)。那當數(shù)據(jù)到了尾部如何處理呢?它將轉(zhuǎn)回到0位置來處理。這個的轉(zhuǎn)回是通過取模操作來執(zhí)行的。

因此環(huán)列隊列的是邏輯上將數(shù)組元素q[0]與q[MAXN-1]連接,形成一個存放隊列的環(huán)形空間。

為了方便讀寫,還要用數(shù)組下標來指明隊列的讀寫位置。head/tail.其中head指向可以讀的位置,tail指向可以寫的位置。 

 

 

[[181482]] 

環(huán)形隊列的關鍵是判斷隊列為空,還是為滿。當tail追上head時,隊列為滿時,當head追上tail時,隊列為空。但如何知道誰追上誰。還需要一些輔助的手段來判斷.

如何判斷環(huán)形隊列為空,為滿有兩種判斷方法。

(1)是附加一個標志位tag

當head趕上tail,隊列空,則令tag=0,

當tail趕上head,隊列滿,則令tag=1,

(2)限制tail趕上head,即隊尾結(jié)點與隊首結(jié)點之間至少留有一個元素的空間。

隊列空: head==tail

隊列滿: (tail+1)% MAXN ==head 

[[181483]]

 

4、程序編寫

  1. #include <linux/module.h> 
  2. #include<linux/kernel.h> 
  3. #include<linux/fs.h> 
  4. #include<linux/init.h> 
  5. #include<linux/delay.h> 
  6. #include<asm/uaccess.h> 
  7. #include<asm/irq.h> 
  8. #include<asm/io.h> 
  9. #include<asm/arch/regs-gpio.h> 
  10. #include<asm/hardware.h> 
  11. #include<linux/proc_fs.h> 
  12.  
  13. #define MYLOG_BUF_LEN 1024 
  14. static char mylog_buf[MYLOG_BUF_LEN]; 
  15. static char tmp_buf[MYLOG_BUF_LEN]; 
  16. static int mylog_r = 0; 
  17. static int mylog_w = 0; 
  18. static int mylog_r_tmp = 0; 
  19.  
  20. /*休眠隊列初始化*/ 
  21. static DECLARE_WAIT_QUEUE_HEAD(mymsg_waitq); 
  22.  
  23. /* 
  24. *判斷環(huán)形隊列是否為空 
  25. *返回0:表示不空  返回1:表示空 
  26. */ 
  27. static int is_mylog_empty(void) 
  28.    return (mylog_r == mylog_w); 
  29.  
  30. /* 
  31. *判斷環(huán)形隊列是否滿 
  32. *返回0:表示不滿  返回1:表示滿 
  33. */ 
  34. static int is_mylog_full(void) 
  35.    return((mylog_w + 1)% MYLOG_BUF_LEN == mylog_r); 
  36.  
  37. /* 
  38. *在讀取的時候,判斷環(huán)形隊列中數(shù)據(jù)是否為空 
  39. *返回0:表示不空  返回1:表示空 
  40. */ 
  41. static int is_mylog_empty_for_read(void) 
  42.    return (mylog_r_tmp == mylog_w); 
  43.  
  44. /* 
  45. *往循環(huán)隊列中存字符 
  46. *輸入:c字符 單位:1byte 
  47. *輸出:無 
  48. */ 
  49. static void mylog_putc(char c) 
  50.  
  51.    if(is_mylog_full()) 
  52.     { 
  53.        /*如果檢測到隊列已經(jīng)滿了,則丟棄該數(shù)據(jù)*/ 
  54.         mylog_r= (mylog_r + 1) % MYLOG_BUF_LEN; 
  55.          
  56.        /*mylog_r_tmp不能大于mylog_r*/ 
  57.         if((mylog_r_tmp + 1)% MYLOG_BUF_LEN == mylog_r) 
  58.             mylog_r_tmp= mylog_r; 
  59.          
  60.     } 
  61.     mylog_buf[mylog_w]= c; 
  62.    /*當mylog_w=1023的時候 (mylog_w+1) % MYLOG_BUF_LEN =0,回到隊列頭,實現(xiàn)循環(huán)*/ 
  63.     mylog_w= (mylog_w + 1) % MYLOG_BUF_LEN; 
  64.    /* 喚醒等待數(shù)據(jù)的進程*/     
  65.     wake_up_interruptible(&mymsg_waitq);   
  66.  
  67. /* 
  68. *從循環(huán)隊列中讀字符 
  69. *輸入:*p 單位:1byte 
  70. *輸出:1表示成功 
  71. */ 
  72. static int mylog_getc(char *p) 
  73.    /*判斷數(shù)據(jù)是否為空*/ 
  74.     if (is_mylog_empty_for_read()) 
  75.     { 
  76.        return 0; 
  77.     } 
  78.    *p = mylog_buf[mylog_r_tmp ]; 
  79.     mylog_r_tmp = (mylog_r_tmp  + 1) % MYLOG_BUF_LEN; 
  80.    return 1; 
  81.  
  82. /* 
  83. *調(diào)用myprintk,和printf用法相同 
  84. */ 
  85. int myprintk(const char *fmt, ...) 
  86.     va_list args; 
  87.    int i; 
  88.    int j; 
  89.  
  90.     va_start(args, fmt); 
  91.     i= vsnprintf(tmp_buf, INT_MAX, fmt, args); 
  92.     va_end(args); 
  93.      
  94.    for (j = 0; j < i; j++) 
  95.         mylog_putc(tmp_buf[j]); 
  96.          
  97.    return i; 
  98.  
  99.  
  100. static ssize_t mymsg_read(struct file *file, char __user *buf, 
  101.             size_t count, loff_t*ppos) 
  102.    int error=0; 
  103.     size_t i=0; 
  104.    char c; 
  105.    /* 把mylog_buf的數(shù)據(jù)copy_to_user, return*/ 
  106.  
  107.     /*非阻塞 和 緩沖區(qū)為空的時候返回*/ 
  108.     if ((file->f_flags & O_NONBLOCK) && is_mylog_empty()) 
  109.        return -EAGAIN; 
  110.      
  111.    /*休眠隊列wait_event_interruptible(xxx,0)-->休眠*/ 
  112.     error= wait_event_interruptible(mymsg_waitq, !is_mylog_empty_for_read()); 
  113.      
  114.    /* copy_to_user*/ 
  115.     while (!error && (mylog_getc(&c)) && i < count) { 
  116.         error= __put_user(c, buf); 
  117.         buf++; 
  118.         i++; 
  119.     } 
  120.    if (!error) 
  121.         error= i; 
  122.    /*返回實際讀到的個數(shù)*/ 
  123.     return error; 
  124.  
  125. static int mymsg_open(struct inode * inode, struct file * file) 
  126.     mylog_r_tmp= mylog_r; 
  127.    return 0; 
  128.  
  129.  
  130. const struct file_operations proc_mymsg_operations = { 
  131.     .read= mymsg_read, 
  132.     .open= mymsg_open, 
  133.     }; 
  134. static int mymsg_init(void) 
  135.    struct proc_dir_entry *myentry; kmsg 
  136.     myentry= create_proc_entry("mymsg", S_IRUSR, &proc_root); 
  137.    if (myentry) 
  138.         myentry->proc_fops = &proc_mymsg_operations; 
  139.    return 0; 
  140.  
  141. static void mymsg_exit(void) 
  142.     remove_proc_entry("mymsg", &proc_root); 
  143.  
  144. module_init(mymsg_init); 
  145. module_exit(mymsg_exit); 
  146.  
  147. /*聲名到內(nèi)核空間*/ 
  148. EXPORT_SYMBOL(myprintk); 
  149.  
  150. MODULE_LICENSE("GPL");  

5、測試程序

注意:在上面程序中 使用了 EXPORT_SYMBOL(myprintk);意思是把myprintk可以在整個內(nèi)核空間使用。

使用方法:

①extern int myprintk(const char *fmt, ...);聲明

② myprintk("first_drv_open : %d\n", ++cnt);使用

  1. #include <linux/module.h> 
  2. #include<linux/kernel.h> 
  3. #include<linux/fs.h> 
  4. #include<linux/init.h> 
  5. #include<linux/delay.h> 
  6. #include<asm/uaccess.h> 
  7. #include<asm/irq.h> 
  8. #include<asm/io.h> 
  9. #include<asm/arch/regs-gpio.h> 
  10. #include<asm/hardware.h> 
  11.  
  12. static struct class *firstdrv_class; 
  13. static struct class_device    *firstdrv_class_dev; 
  14.  
  15. volatile unsigned long *gpfcon = NULL
  16. volatile unsigned long *gpfdat = NULL
  17.  
  18. extern int myprintk(const char *fmt, ...); 
  19.  
  20. static int first_drv_open(struct inode *inode, struct file *file) 
  21.    static int cnt = 0; 
  22.     myprintk("first_drv_open : %d\n", ++cnt); 
  23.    /* 配置GPF4,5,6為輸出*/ 
  24.     *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2))); 
  25.    *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2))); 
  26.    return 0; 
  27.  
  28. static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) 
  29.    int val; 
  30.    static int cnt = 0; 
  31.  
  32.     myprintk("first_drv_write : %d\n", ++cnt); 
  33.  
  34.     copy_from_user(&val, buf, count); //    copy_to_user(); 
  35.  
  36.     if (val == 1) 
  37.     { 
  38.        // 點燈 
  39.         *gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); 
  40.     } 
  41.    else 
  42.     { 
  43.        // 滅燈 
  44.         *gpfdat |= (1<<4) | (1<<5) | (1<<6); 
  45.     } 
  46.      
  47.    return 0; 
  48.  
  49. static struct file_operations first_drv_fops = { 
  50.     .owner =  THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量*/ 
  51.     .open  =  first_drv_open,     
  52.     .write   =    first_drv_write,       
  53. }; 
  54.  
  55.  
  56. int major; 
  57. static int first_drv_init(void) 
  58.     myprintk("first_drv_init\n"); 
  59.     major= register_chrdev(0, "first_drv", &first_drv_fops); // 注冊, 告訴內(nèi)核 
  60.  
  61.     firstdrv_class= class_create(THIS_MODULE, "firstdrv"); 
  62.  
  63.     firstdrv_class_dev= class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL"xyz"); /* /dev/xyz*/ 
  64.  
  65.     gpfcon= (volatile unsigned long *)ioremap(0x56000050, 16); 
  66.     gpfdat= gpfcon + 1; 
  67.  
  68.    return 0; 
  69.  
  70. static void first_drv_exit(void) 
  71.     unregister_chrdev(major,"first_drv"); // 卸載 
  72.  
  73.     class_device_unregister(firstdrv_class_dev); 
  74.     class_destroy(firstdrv_class); 
  75.     iounmap(gpfcon); 
  76.  
  77. module_init(first_drv_init); 
  78. module_exit(first_drv_exit); 
  79.  
  80.  
  81. MODULE_LICENSE("GPL");  

6、在tty中測試效果

  1. # insmod my_msg.ko 
  2. # insmod first_drv.ko 
  3. # cat /proc/mymsg 
  4. mymsg_open mylog_r_tmp=0 
  5. first_drv_init  
責任編輯:龐桂玉 來源: 嵌入式Linux中文站
相關推薦

2017-03-30 10:13:11

Linux內(nèi)核文件系統(tǒng)

2016-08-23 09:17:08

LinuxD狀態(tài)TASK_RUNNIN

2010-01-22 11:01:04

linux內(nèi)核模塊

2014-08-28 15:08:35

Linux內(nèi)核

2009-12-18 16:08:17

Fedora proc

2022-02-08 15:15:26

OpenHarmonlinux鴻蒙

2021-07-11 06:45:18

Linux內(nèi)核靜態(tài)

2021-04-15 05:51:25

Linux

2023-04-10 09:44:22

內(nèi)核鼠標調(diào)試鴻蒙

2021-07-10 08:29:13

Docker內(nèi)核Namespace

2010-06-02 09:31:43

Linux core

2023-02-28 09:47:42

2022-03-03 18:18:53

BPF解釋器系統(tǒng)

2021-11-14 07:29:55

Linux 內(nèi)核靜態(tài)追蹤Linux 系統(tǒng)

2021-11-15 04:00:07

Linux 內(nèi)核動態(tài)

2021-07-14 10:33:22

Docker內(nèi)核Mount Names

2023-03-01 23:56:11

2009-10-27 15:06:15

Linux內(nèi)核啟動

2023-03-01 23:53:30

Linuxshutdown進程

2023-03-10 14:56:37

Linuxconnect系統(tǒng)
點贊
收藏

51CTO技術棧公眾號