From f5d83411b0cacce04eb2c08eb7c87a1e87bc75f7 Mon Sep 17 00:00:00 2001 From: gatieme Date: Mon, 16 May 2016 22:03:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0print=5Fvmarea=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8=E4=BB=A3=E7=A0=81...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernel/process/task/list_process/Makefile | 6 +- .../process/task/list_process/list_process.c | 3 +- .../kernel/process/task/print_vmarea/Makefile | 18 +++ .../process/task/print_vmarea/print_vmarea.c | 128 ++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 study/kernel/process/task/print_vmarea/Makefile create mode 100644 study/kernel/process/task/print_vmarea/print_vmarea.c diff --git a/study/kernel/process/task/list_process/Makefile b/study/kernel/process/task/list_process/Makefile index f1b0236..3fab39e 100644 --- a/study/kernel/process/task/list_process/Makefile +++ b/study/kernel/process/task/list_process/Makefile @@ -1,7 +1,9 @@ - +ifneq ($(KERNELRELEASE),) obj-m := list_process.o +else + KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) @@ -12,3 +14,5 @@ all: clean: make -C $(KERNELDIR) M=$(PWD) clean + +endif diff --git a/study/kernel/process/task/list_process/list_process.c b/study/kernel/process/task/list_process/list_process.c index 053632f..7fdab62 100644 --- a/study/kernel/process/task/list_process/list_process.c +++ b/study/kernel/process/task/list_process/list_process.c @@ -81,7 +81,6 @@ void do_list_process(void) printk(KERN_ALERT "The method is %s\n", method); printk(KERN_ALERT "there are %d process in your system now...\n", count); printk(KERN_ALERT "current : %d\t%s\n", current->pid, current->comm); - return 0; } @@ -90,6 +89,8 @@ static int init_list_process(void) { // list all the process of you system do_list_process( ); + + return 0; } static void exit_list_process(void) diff --git a/study/kernel/process/task/print_vmarea/Makefile b/study/kernel/process/task/print_vmarea/Makefile new file mode 100644 index 0000000..7e5233b --- /dev/null +++ b/study/kernel/process/task/print_vmarea/Makefile @@ -0,0 +1,18 @@ +ifneq ($(KERNELRELEASE),) + +obj-m := print_vmarea.o + +else + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build + +PWD := $(shell pwd) + +all: + make -C $(KERNELDIR) M=$(PWD) modules + +clean: + make -C $(KERNELDIR) M=$(PWD) clean + + +endif diff --git a/study/kernel/process/task/print_vmarea/print_vmarea.c b/study/kernel/process/task/print_vmarea/print_vmarea.c new file mode 100644 index 0000000..2784ffc --- /dev/null +++ b/study/kernel/process/task/print_vmarea/print_vmarea.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef offsetof +#define offsetof(type, field) ((long) &((type *)0)->field) +#endif /* offsetof */ + +#ifndef container_of +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif + + +static int pid = 1; + +module_param(pid,int,0644); + +static void printvm_list(void) +{ + struct task_struct *p; + struct pid *k; + struct vm_area_struct *tmp; + + k = find_vpid(pid); + p = pid_task(k,PIDTYPE_PID); + tmp = p->mm->mmap; + + printk("process:%s,pid:%d\n",p->comm,p->pid); + + while (tmp != NULL) { + printk("0x%lx - 0x%lx\t",tmp->vm_start,tmp->vm_end); + if (tmp->vm_flags & VM_READ) + printk("r"); + else + printk("-"); + + if (tmp->vm_flags & VM_WRITE) + printk("w"); + else + printk("-"); + + if (tmp->vm_flags & VM_EXEC) + printk("x"); + else + printk("-"); + + if (tmp->vm_flags & VM_SHARED) + printk("s\n"); + else + printk("p\n"); + + tmp = tmp->vm_next; + } +} + + + +static void visit(struct rb_node *root) +{ + struct vm_area_struct *tmp; + tmp = container_of(root,struct vm_area_struct,vm_rb); + printk("0x%lx - 0x%lx\t",tmp->vm_start,tmp->vm_end); + if (tmp->vm_flags & VM_READ) + printk("r"); + else + printk("-"); + + if (tmp->vm_flags & VM_WRITE) + printk("w"); + else + printk("-"); + + if (tmp->vm_flags & VM_EXEC) + printk("x"); + else + printk("-"); + + if (tmp->vm_flags & VM_SHARED) + printk("s\n"); + else + printk("p\n"); +} + +static void print_rb_tree(struct rb_node *root) +{ + if (root != NULL) { + visit(root); + print_rb_tree(root->rb_left); + print_rb_tree(root->rb_right); + } +} + +static void printvm_tree(void) +{ + struct task_struct *p; + struct pid *k; + struct rb_node *root = NULL; + + k = find_vpid(pid); + p = pid_task(k,PIDTYPE_PID); + root = p->mm->mm_rb.rb_node; + print_rb_tree(root); + return ; +} + +static int __init printvm_init(void) +{ + printvm_list(); + printk("------------------------\n"); + printvm_tree(); + return 0; +} + +static void __exit printvm_exit(void) +{ + printk("<1>exit ---------------------!\n"); +} + +module_init(printvm_init); +module_exit(printvm_exit); +MODULE_LICENSE("GPL");