mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-09-21 03:43:38 +08:00
debugfs...
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
obj-m := jif.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
#KERNELDIR ?= /home/gatieme/Work/Kernel/linux-headers-3.14-desktop/
|
||||
PWD := $(shell pwd)
|
||||
|
||||
all:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
gcc test.c -o test
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -rf ./test
|
||||
|
||||
endif
|
||||
@@ -0,0 +1,8 @@
|
||||
http://blog.sina.com.cn/s/blog_8795b0970101ij5d.html
|
||||
写入proc文件
|
||||
echo "Hello from kernel" /proc/proc_test/hello'
|
||||
读取proc文件内容,将看到屏幕上显示了我们写入的字符串:Hello from kernel
|
||||
cat /proc/proc_test/hello
|
||||
|
||||
|
||||
http://blog.csdn.net/a_ran/article/details/37630101
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/sched.h>
|
||||
#include <asm/uaccess.h>
|
||||
|
||||
#define STRINGLEN 81
|
||||
char global_buffer[STRINGLEN];
|
||||
|
||||
struct proc_dir_entry *example_dir, *proc_hello_file;
|
||||
|
||||
int proc_read_hello(char *page, char **start, off_t off, int count, int *eof, void *data);
|
||||
int proc_write_hello(struct file *file, const char *buffer, unsigned long count, void *data);
|
||||
|
||||
|
||||
|
||||
|
||||
int proc_read_hello(char *page, char **start, off_t off, int count, int *eof,
|
||||
void *data)
|
||||
{
|
||||
int len;
|
||||
len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者
|
||||
printk("read success : len = %d, buff = %s\n", len, global_buffer);
|
||||
return len;
|
||||
}
|
||||
int proc_read_hello(struct file *file, char __user *buff, size_t size, loff_t *ppos)
|
||||
|
||||
int proc_write_hello(struct file *file, const char *buffer, unsigned long count,
|
||||
void *data)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (count = STRINGLEN)
|
||||
{
|
||||
len = STRINGLEN - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = count;
|
||||
}
|
||||
|
||||
copy_from_user(global_buffer, buffer, len);
|
||||
global_buffer[len] = '\0';
|
||||
printk("write success : len = %d, buff = %s\n", len, global_buffer);
|
||||
return len;
|
||||
}
|
||||
|
||||
static int __init proc_test_init(void)
|
||||
{
|
||||
example_dir = proc_mkdir("proc_test", NULL);
|
||||
printk("Create /proc/proc_test success...\n");
|
||||
//hello_file = create_proc_entry("hello", S_IRUGO, example_dir);
|
||||
//strcpy(global_buffer, "hello");
|
||||
//hello_file->read_proc = proc_read_hello;
|
||||
//hello_file->write_proc = proc_write_hello;
|
||||
|
||||
static const struct file_operations pid_fops =
|
||||
{
|
||||
.owner = THIS_MODULE,
|
||||
.read = proc_read_hello,
|
||||
.write = proc_write_hello,
|
||||
};
|
||||
proc_hello_file = proc_create("hello", 0666, example_dir, &pid_fops);
|
||||
|
||||
if(proc_hello_file == NULL)
|
||||
{
|
||||
printk("Can't create /proc/proc_test/hello\n");
|
||||
remove_proc_entry("proc_test", NULL);
|
||||
}
|
||||
printk("Create /proc/proc_test/hello success...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit proc_test_exit(void)
|
||||
{
|
||||
remove_proc_entry("hello", example_dir);
|
||||
remove_proc_entry("proc_test", NULL);
|
||||
}
|
||||
|
||||
module_init(proc_test_init);
|
||||
module_exit(proc_test_exit);
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/sched.h>
|
||||
#include <asm/uaccess.h>
|
||||
|
||||
#define STRINGLEN 81
|
||||
char global_buffer[STRINGLEN];
|
||||
|
||||
struct proc_dir_entry *example_dir, *proc_hello_file;
|
||||
|
||||
int proc_read_hello(char *page, char **start, off_t off, int count, int *eof, void *data);
|
||||
int proc_write_hello(struct file *file, const char *buffer, unsigned long count, void *data);
|
||||
|
||||
|
||||
|
||||
|
||||
int proc_read_hello(char *page, char **start, off_t off, int count, int *eof,
|
||||
void *data)
|
||||
{
|
||||
int len;
|
||||
len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者
|
||||
printk("read success : len = %d, buff = %s\n", len, global_buffer);
|
||||
return len;
|
||||
}
|
||||
int proc_read_hello(struct file *file, char __user *buff, size_t size, loff_t *ppos)
|
||||
int proc_write_hello(struct file *file, const char *buffer, unsigned long count,
|
||||
void *data)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (count = STRINGLEN)
|
||||
{
|
||||
len = STRINGLEN - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = count;
|
||||
}
|
||||
|
||||
copy_from_user(global_buffer, buffer, len);
|
||||
global_buffer[len] = '\0';
|
||||
printk("write success : len = %d, buff = %s\n", len, global_buffer);
|
||||
return len;
|
||||
}
|
||||
|
||||
static int __init proc_test_init(void)
|
||||
{
|
||||
example_dir = proc_mkdir("proc_test", NULL);
|
||||
printk("Create /proc/proc_test success...\n");
|
||||
//hello_file = create_proc_entry("hello", S_IRUGO, example_dir);
|
||||
//strcpy(global_buffer, "hello");
|
||||
//hello_file->read_proc = proc_read_hello;
|
||||
//hello_file->write_proc = proc_write_hello;
|
||||
|
||||
static const struct file_operations pid_fops =
|
||||
{
|
||||
.owner = THIS_MODULE,
|
||||
.read = proc_read_hello,
|
||||
.write = proc_write_hello,
|
||||
};
|
||||
proc_hello_file = proc_create("hello", 0666, example_dir, &pid_fops);
|
||||
|
||||
if(proc_hello_file == NULL)
|
||||
{
|
||||
printk("Can't create /proc/proc_test/hello\n");
|
||||
remove_proc_entry("proc_test", NULL);
|
||||
}
|
||||
printk("Create /proc/proc_test/hello success...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit proc_test_exit(void)
|
||||
{
|
||||
remove_proc_entry("hello", example_dir);
|
||||
remove_proc_entry("proc_test", NULL);
|
||||
}
|
||||
|
||||
module_init(proc_test_init);
|
||||
module_exit(proc_test_exit);
|
||||
@@ -0,0 +1,69 @@
|
||||
#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文件的读函数
|
||||
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;
|
||||
}
|
||||
|
||||
//proc文件的写函数
|
||||
static int my_proc_write(struct file *filp, const char __user *buf, unsigned long count, void *data)
|
||||
{
|
||||
//分配临时缓冲区
|
||||
char *tmp = kzalloc((count+1), GFP_KERNEL);
|
||||
if (!tmp)
|
||||
return -ENOMEM;
|
||||
|
||||
//将用户态write的字符串拷贝到内核空间
|
||||
//copy_to|from_user(to,from,cnt)
|
||||
if (copy_from_user(tmp, buf, count)) {
|
||||
kfree(tmp);
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
//将str的旧空间释放,然后将tmp赋值给str
|
||||
kfree(str);
|
||||
str = tmp;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
static int __init my_init(void)
|
||||
{
|
||||
struct proc_dir_entry *file;
|
||||
|
||||
//创建proc文件
|
||||
file = create_proc_entry("jif", 0666, NULL);
|
||||
if (!file) {
|
||||
printk("Cannot create /proc/jif\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//将创建好的文件和读写函数关联在一起
|
||||
file->read_proc = my_proc_read;
|
||||
file->write_proc = my_proc_write;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit my_exit(void)
|
||||
{
|
||||
//删除proc文件
|
||||
remove_proc_entry("jif", NULL);
|
||||
kfree(str);
|
||||
}
|
||||
|
||||
module_init(my_init);
|
||||
module_exit(my_exit);
|
||||
MODULE_AUTHOR("aran");
|
||||
MODULE_LICENSE("GPL");
|
||||
@@ -0,0 +1,61 @@
|
||||
/*************************************************************************
|
||||
> File Name: test.c
|
||||
> Author: gatieme
|
||||
> Created Time: 2016年05月24日 星期二 19时45分38秒
|
||||
************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#define PROC_FILE "/proc/proc_test/hello"
|
||||
#define MAX_LINE 81
|
||||
|
||||
int main( )
|
||||
{
|
||||
int ret;
|
||||
int procFile;
|
||||
char buff[MAX_LINE];
|
||||
|
||||
//wait for ack signal
|
||||
procFile = open(PROC_FILE, O_RDWR);
|
||||
if(procFile == -1)
|
||||
{
|
||||
perror("Fail to open "PROC_FILE);
|
||||
exit(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Open success...\n");
|
||||
}
|
||||
|
||||
if((ret = write(procFile, "hello", strlen("hello"))) == -1)
|
||||
{
|
||||
perror("Fail to read "PROC_FILE);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("wriet success\n");
|
||||
}
|
||||
|
||||
bzero(buff, sizeof(buff));
|
||||
if((ret = read(procFile, buff, MAX_LINE)) == -1)
|
||||
{
|
||||
perror("Fail to read "PROC_FILE);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("buff = %s\n", buff);
|
||||
}
|
||||
|
||||
close(procFile);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*************************************************************************
|
||||
> File Name: test.c
|
||||
> Author: gatieme
|
||||
> Created Time: 2016年05月24日 星期二 19时45分38秒
|
||||
************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#define PROC_FILE "/proc/proc_test/hello"
|
||||
#define MAX_LINE 81
|
||||
|
||||
int main( )
|
||||
{
|
||||
int ret;
|
||||
int count;
|
||||
int procFile;
|
||||
char buff[MAX_LINE];
|
||||
|
||||
bzero(buff, sizeof(buff));
|
||||
sprintf(buff, "echo \"hello\" > %s", PROC_FILE);
|
||||
printf("%s\n", buff);
|
||||
system(buff);
|
||||
printf("write success...\n");
|
||||
//wait for ack signal
|
||||
procFile = open(PROC_FILE, O_RDONLY);
|
||||
if(procFile == -1)
|
||||
{
|
||||
perror("Fail to open "PROC_FILE);
|
||||
exit(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Open success...");
|
||||
}
|
||||
|
||||
bzero(buff, sizeof(buff));
|
||||
ret = read(procFile, buff, MAX_LINE);
|
||||
if(ret == -1)
|
||||
{
|
||||
perror("Fail to read "PROC_FILE);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("buff = %s\n", buff);
|
||||
}
|
||||
|
||||
close(procFile);
|
||||
}
|
||||
Reference in New Issue
Block a user