Linux内核调试技术之自构proc

简介:

1、简介

在内核中使用printk可以讲调试信息保存在log_buf缓冲区中,可以使用命令 #cat /proc/kmsg 将缓冲区的数区的数数据打印出来,今天我们就来研究一下,自己写kmsg这个文件,我们取名叫做 mymsg。

2、查看内核中 /proc/kmsg怎么写的!

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

  
  
  1. void __init proc_misc_init(void){ 
  2.     ......................... 
  3.         struct proc_dir_entry *entry; 
  4.         //这里创建了一个proc入口kmsg 
  5.         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root); 
  6.         if (entry) /*构造一个proc_fops结构*/ 
  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的时候,会调用kmsg_open,在调用kmsg_read函数,读取log_buf中的数据,拷贝到用户空间显示。

3、在写之前,我们需要来学习一下循环队列

环形队列是在实际编程极为有用的数据结构,它有如下特点:

  • 它是一个首尾相连的FIFO的数据结构,采用数组的线性空间,数据组织简单,能很快知道队列是否满为空。能以很快速度的来存取数据。
  • 因为有简单高效的原因,甚至在硬件都实现了环形队列。
  • 环形队列广泛用于网络数据收发,和不同程序间数据交换(比如内核与应用程序大量交换数据,从硬件接收大量数据)均使用了环形队列。

3.1.环形队列实现原理

内存上没有环形的结构,因此环形队列实上是数组的线性空间来实现。那当数据到了尾部如何处理呢?它将转回到0位置来处理。这个的转回是通过取模操作来执行的。

因此环列队列的是逻辑上将数组元素q[0]与q[MAXN-1]连接,形成一个存放队列的环形空间。

为了方便读写,还要用数组下标来指明队列的读写位置。head/tail.其中head指向可以读的位置,tail指向可以写的位置。

环形队列

环形队列的关键是判断队列为空,还是为满。当tail追上head时,队列为满时,当head追上tail时,队列为空。但如何知道谁追上谁。还需要一些辅助的手段来判断.

如何判断环形队列为空,为满有两种判断方法。

(1)是附加一个标志位tag

当head赶上tail,队列空,则令tag=0,

当tail赶上head,队列满,则令tag=1,

(2)限制tail赶上head,即队尾结点与队首结点之间至少留有一个元素的空间。

队列空: head==tail

队列满: (tail+1)% MAXN ==head

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. *判断环形队列是否为空 
  25. *返回0:表示不空  返回1:表示空 
  26. */ 
  27. static int is_mylog_empty(void) 
  28.    return (mylog_r == mylog_w); 
  29.  
  30. /* 
  31. *判断环形队列是否满 
  32. *返回0:表示不满  返回1:表示满 
  33. */ 
  34. static int is_mylog_full(void) 
  35.    return((mylog_w + 1)% MYLOG_BUF_LEN == mylog_r); 
  36.  
  37. /* 
  38. *在读取的时候,判断环形队列中数据是否为空 
  39. *返回0:表示不空  返回1:表示空 
  40. */ 
  41. static int is_mylog_empty_for_read(void) 
  42.    return (mylog_r_tmp == mylog_w); 
  43.  
  44. /* 
  45. *往循环队列中存字符 
  46. *输入:c字符 单位:1byte 
  47. *输出:无 
  48. */ 
  49. static void mylog_putc(char c) 
  50.  
  51.    if(is_mylog_full()) 
  52.     { 
  53.        /*如果检测到队列已经满了,则丢弃该数据*/ 
  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,回到队列头,实现循环*/ 
  63.     mylog_w= (mylog_w + 1) % MYLOG_BUF_LEN; 
  64.    /* 唤醒等待数据的进程*/     
  65.     wake_up_interruptible(&mymsg_waitq);   
  66.  
  67. /* 
  68. *从循环队列中读字符 
  69. *输入:*p 单位:1byte 
  70. *输出:1表示成功 
  71. */ 
  72. static int mylog_getc(char *p) 
  73.    /*判断数据是否为空*/ 
  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. *调用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的数据copy_to_user, return*/ 
  106.  
  107.     /*非阻塞 和 缓冲区为空的时候返回*/ 
  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.    /*返回实际读到的个数*/ 
  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. /*声名到内核空间*/ 
  148. EXPORT_SYMBOL(myprintk); 
  149.  
  150. MODULE_LICENSE("GPL");  

5、测试程序

注意:在上面程序中 使用了 EXPORT_SYMBOL(myprintk);意思是把myprintk可以在整个内核空间使用。

使用方法:

①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,    /* 这是一个宏,推向编译模块时自动创建的__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); // 注册, 告诉内核 
  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  




本文作者:佚名
来源:51CTO
目录
打赏
0
0
0
0
26199
分享
相关文章
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
131 67
|
12天前
|
Linux内核中的current机制解析
总的来说,current机制是Linux内核中进程管理的基础,它通过获取当前进程的task_struct结构的地址,可以方便地获取和修改进程的信息。这个机制在内核中的使用非常广泛,对于理解Linux内核的工作原理有着重要的意义。
36 11
Linux 内核源码分析---proc 文件系统
`proc`文件系统是Linux内核中一个灵活而强大的工具,提供了一个与内核数据结构交互的接口。通过本文的分析,我们深入探讨了 `proc`文件系统的实现原理,包括其初始化、文件的创建与操作、动态内容生成等方面。通过对这些内容的理解,开发者可以更好地利用 `proc`文件系统来监控和调试内核,同时也为系统管理提供了便利的工具。
76 16
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
Intel Linux 内核测试套件-LKVS介绍 | 龙蜥大讲堂104期
《Intel Linux内核测试套件-LKVS介绍》(龙蜥大讲堂104期)主要介绍了LKVS的定义、使用方法、测试范围、典型案例及其优势。LKVS是轻量级、低耦合且高代码覆盖率的测试工具,涵盖20多个硬件和内核属性,已开源并集成到多个社区CICD系统中。课程详细讲解了如何使用LKVS进行CPU、电源管理和安全特性(如TDX、CET)的测试,并展示了其在实际应用中的价值。
云上体验最佳的服务器操作系统 - Alibaba Cloud Linux | 飞天技术沙龙-CentOS 迁移替换专场
本次方案的主题是云上体验最佳的服务器操作系统 - Alibaba Cloud Linux ,从 Alibaba Cloud Linux 的产生背景、产品优势以及云上用户使用它享受的技术红利等方面详细进行了介绍。同时,通过国内某社交平台、某快递企业、某手机客户大数据业务 3 大案例,成功助力客户实现弹性扩容能力提升、性能提升、降本增效。 1. 背景介绍 2. 产品介绍 3. 案例分享
Ubuntu20.04搭建嵌入式linux网络加载内核、设备树和根文件系统
使用上述U-Boot命令配置并启动嵌入式设备。如果配置正确,设备将通过TFTP加载内核和设备树,并通过NFS挂载根文件系统。
226 15
|
4月前
|
linux之core文件如何查看和调试
通过设置和生成 core 文件,可以在程序崩溃时获取详细的调试信息。结合 GDB 等调试工具,可以深入分析 core 文件,找到程序崩溃的具体原因,并进行相应的修复。掌握这些调试技巧,对于提高程序的稳定性和可靠性具有重要意义。
1316 6
Linux设备驱动程序(四)——调试技术3
Linux设备驱动程序(四)——调试技术3
207 0
Linux设备驱动程序(四)——调试技术1
Linux设备驱动程序(四)——调试技术1
219 0
下一篇
oss创建bucket
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等