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).
--------------------------------------------------------------------------------------------------------------- -----
Two
PROC_TEST02.C Source
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/fs.h >//for basic filesystem#include <linux/proc_fs.h>//for the proc filesystem#include <linux/seq_file.h>/ /For sequence files#include <linux/jiffies.h>//for jiffies#include <linux/slab.h>//for Kzalloc, kfree# Include <linux/uaccess.h>//for Copy_from_user#define buf_size 128//global varstatic char *str = null;//Seq_operat Ions-showstatic int jif_show (struct seq_file *m, void *v) {char buf[buf_size];int ret = 0;ret = sprintf (buf, "current Kernel time is%llu\n ", (unsigned long Long) get_jiffies_64 ()); ret + = sprintf (buf + ret," str is%s\n ", str); seq_printf (M , "%s", buf); return 0; //!! Must be 0, or would show nothing t.t}//file_operations-writestatic ssize_t jif_write (struct file *file, const char _ _user *buffer, size_t count, loff_t *f_pos) {//Allocate temporary buffer char *tmp = Kzalloc ((count+1), gfp_kernel); if (!tmp) return-enomem;/ /user-state write characterstring copy to Kernel space//copy_to|from_user (to,from,cnt) if (Copy_from_user (TMP, buffer, count)) {Kfree (TMP); return-efault;} The old space of Str is freed and the TMP is assigned to Strkfree (str); str = Tmp;return count;} Seq_operations-openstatic int jif_open (struct inode *inode, struct file *file) {return Single_open (file, Jif_show, NULL);} static const struct File_operations jif_fops = {. owner= this_module,.open= jif_open,.read= seq_read,.write = jif_write,.l lseek= seq_lseek,.release= single_release,};//Module initstatic int __init jif_init (void) {struct proc_dir_entry* jif_ File;jif_file = Proc_create ("Jif", 0, NULL, &jif_fops); if (null = = Jif_file) {return-enomem;} return 0;} module exitstatic void __exit jif_exit (void) {Remove_proc_entry ("Jif", NULL); Kfree (str);} Module_init (Jif_init); Module_exit (Jif_exit); Module_author ("Aran"); Module_license ("GPL");
Makefile File:
obj-m:= proc_test02.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: