Driver Debugging Method printk--homemade proc file (ii)

Source: Internet
Author: User

The procedure in the previous section was exciting, and we implemented a MYPRINTK printing function ourselves. But there is a fatal flaw in this function, which is that you can only use the CAT/PROC/MYMSG command once to read the value of Mylog_buf. This is because the read to the end will appear: Mylog_r = = Mylog_w, indicating that the buffer is empty, the next time you can not read the data. In this section we are going to solve this problem, and all we have to do is print from scratch every time we use cat/proc/mymsg. Then we need to make a copy of the entry, one to save and one to transform. In this case, the next time you read, we can make a copy of the saved portal and then let the copy change. The specific procedures are as follows:

#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/io.h>#include <asm/arch/regs-gpio.h>#include <asm/hardware.h>#include <linux/proc_fs.h> #define Mylog_buf_len 1024x768 struct proc_dir_entry *myentry; static char Mylog_buf[mylog_buf_len];static char Tmp_buf[mylog_buf_len];static int mylog_r = 0;static int mylog_r_for_read = 0;//This is used to copy mylog_r, it will change, but Mylog_r unchangedstatic int mylog_w = 0; static Declare_wait_queue_head (MYMSG_WAITQ); static int is_mylog_empty (void){return (Mylog_r = = mylog_w);} static int is_mylog_empty_for_read (void){return (Mylog_r_for_read = = mylog_w);} static int is_mylog_full (void){return ((mylog_w + 1)% Mylog_buf_len = = mylog_r);} //This function is called by the MYPRINTK function.static void Mylog_putc (char c){if (Is_mylog_full ()) {Mylog_r = (mylog_r + 1)% Mylog_buf_len; /* The reason for adding the following three lines is that if you are reading, you have been calling Printk to write,* When writing faster, it may lead to mylog_w more than Mylog_r_for_read,* You need to update mylog_r_for_read so that Mylog_r_for_read points to the new portal* When the mylog_w exceeds the entrance mylog_r, Mylog_r will always follow the update!                */if ((Mylog_r_for_read + 1)% Mylog_buf_len = = mylog_r) {mylog_r_for_read = mylog_r; } } mylog_buf[mylog_w] = c;mylog_w = (mylog_w + 1)% Mylog_buf_len; wake_up_interruptible (&MYMSG_WAITQ);} static intMylog_getc_for_read(char *p){if (Is_mylog_empty_for_read()) {return 0; }*p = mylog_buf[Mylog_r_for_read];Mylog_r_for_read= (Mylog_r_for_read+ 1)% Mylog_buf_len;return 1;}  int MYPRINTK (const char *fmt, ...){va_list args;int i;Int J; va_start (args, fmt);i = vsnprintf (Tmp_buf, Int_max, FMT, args);va_end (args);For (j = 0; J < i; j + +)MYLOG_PUTC(Tmp_buf[j]);return i;} static ssize_t mymsg_read (struct file *file, char __user *buf,size_t count, loff_t *ppos){int error = 0;int i = 0;char c; if ((File->f_flags & O_nonblock) && is_mylog_empty_for_read ())Return-eagain; error = wait_event_interruptible (MYMSG_WAITQ,!is_mylog_empty_for_read ()); / * Copy_to_user * /While (!error && (Mylog_getc_for_read(&c)) && I < count) {error = __put_user (c, buf);buf++;i++; }if (!error)error = i;return error;} static int Mymsg_open (struct inode *inode, struct file *file){Mylog_r_for_read= Mylog_r;return 0;} const struct File_operations proc_mymsg_operations = {. Open = Mymsg_open,. Read = Mymsg_read,}; static int mymsg_init (void){ myentry = create_proc_entry ("mymsg", S_irusr, &proc_root);if (myentry)myentry->proc_fops = &proc_mymsg_operations;return 0;} static void Mymsg_exit (void){remove_proc_entry ("mymsg", &proc_root);} Module_init (mymsg_init);Module_exit (mymsg_exit); Export_symbol (MYPRINTK); module_license ("GPL"); summing up, about this function, when we use the command: cat/proc/mymsg in user space, we first call the Open function, in the open function will make a copy of the entry, and then take one copy as the amount of change, the other as the entrance does not change. This way, every time you cat/proc/mymsg, you will start printing from the entrance!

printk--of Driver Debugging method homemade proc file (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.