When doing kernel-driven development, you can use the files under/proc to get the appropriate information for debugging.
Most of the files under/proc are read-only, but for the completeness of the example, a write method is provided.
Method One: Use Create_proc_entry to create the proc file (simple, but the write operation has the danger of buffer overflow);
Method Two: Use Proc_create and Seq_file to create the proc file (method three concise);
Method Three: Use Proc_create_data and Seq_file to create the proc file (more cumbersome, but more complete);
Example four: an example of using a kernel-linked list in a proc file (method three).
--------------------------------------------------------------------------------------------------------------- -----
One
PROC_TEST01.C Source
#include <linux/module.h> #include <linux/sched.h>//jiffies#include <linux/proc_fs.h> #include <linux/uaccess.h>//copy_to|from_user () static char *str = Null;//proc file read function static int my_proc_read (char *page, Char **start, off_t off, int count, int *eof, void *data) {int ret = 0;ret = sprintf (page, "Current kernel time is%ld\n", jiffies); ret + = sprintf (Page+ret, "str is%s\n", str); return ret;} Write function for proc file static int my_proc_write (struct file *filp, const char __user *buf, unsigned long count, void *data) {//Allocate temporary buffer Char *tmp = Kzalloc ((count+1), Gfp_kernel), if (!tmp) return-enomem;//copies the user-state write string to the kernel space//copy_to|from_user (to,from , CNT) if (Copy_from_user (TMP, BUF, Count)) {Kfree (TMP); return-efault;} The old space of Str is freed and the TMP is assigned to Strkfree (str); str = Tmp;return count;} static int __init my_init (void) {struct proc_dir_entry *file;//create proc file = Create_proc_entry ("Jif", 0666, NULL); File) {PRINTK ("cannot create/proc/jif\n"); return-1;} Associating a created file with a read-write function File->read_proc = my_proc_read;file->write_proc = My_proc_write;return 0;} static void __exit my_exit (void) {//delete proc file remove_proc_entry ("Jif", NULL); Kfree (str);} Module_init (My_init); Module_exit (My_exit); Module_author ("Aran"); Module_license ("GPL");
Makefile File:
obj-m:= proc_test01.okernel:=/lib/modules/' uname-r '/build #for mint/ubuntu#kernel: =/lib/modules/' uname-r '/source # For Redhatall:make-c $ (KERNEL) m= ' pwd ' modulesinstall:make-c $ (KERNEL) m= ' pwd ' modules_installdepmod-aclean:make-c $ (K Ernel) m= ' pwd ' clean
Test Results:
Because I am using Linxu mint, create_proc_entry () function has been deleted, it cannot be tested on this machine, but tested on redhat6.4, use methods and tests refer to the following method.