This commit is contained in:
gatieme
2016-05-19 15:00:26 +08:00
parent e110daa18e
commit d2dfae4504
14 changed files with 39 additions and 39 deletions
@@ -0,0 +1,37 @@
ifneq ($(KERNELRELEASE),)
#CFG_FLAGS += -O2 -I./
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
MODULE_NAME := find-task
RESMAIN_CORE_OBJS := find_task.o
RESMAIN_GLUE_OBJS := print_task.o print_vmarea.o
find-task-objs := $(RESMAIN_GLUE_OBJS) $(RESMAIN_CORE_OBJS)
obj-m += find-task.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
make -C $(KERNELDIR) M=$(PWD) modules
modules_install:
make -C $(KERNELDIR) M=$(PWD) modules_install
clean:
make -C $(KERNELDIR) M=$(PWD) clean
.PHNOY:
modules modules_install clean
endif
@@ -0,0 +1,174 @@
/*************************************************************************
> File Name: process.c
> Author: GatieMe
> Mail: gatieme@163.com
> Created Time: 2016年04月01日 星期五 21时09分29秒
************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include "print_task.h"
/*
* macro for find_task_by_pid in the process list
* LIST_FOR_EACH to traversing the process linked list by `list_for_each`
* FOR_EACH_PROCESS to traversing the process linked list by `for_each_process`
* LIST_FOR_EACH_ENTRY to traversing the process linked list by `list_for_each_entry`
* LIST_ALL to traversing the process linked list use all above macro or function
*/
#define LIST_ALL 1
#define LIST_FOR_EACH 0
#define FOR_EACH_PROCESS 0
#define LIST_FOR_EACH_ENTRY 0
//#define METHOD 2
static unsigned int METHOD = 1;
module_param(METHOD, uint, 0400);
static unsigned int PID = 1;
module_param(PID, uint, 0400);
void getTaskinfo(struct task_struct *task)
{
}
struct task_struct* find_task_by_pid_in_rbtree(int pid)
{
return NULL;
}
struct task_struct* find_task_by_pid_in_ns(int pid)
{
struct task_struct *pTask = NULL;
printk(KERN_ALERT "pid_task");
pTask = pid_task(find_vpid(pid), PIDTYPE_PID);
if(pTask != NULL)
{
printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, (void *)pTask);
}
return pTask;
}
struct task_struct* find_task_by_pid_in_list(int pid)
{
struct task_struct *task = NULL, *pTask = NULL;
struct list_head *pos = NULL;
task = &init_task;
#if LIST_ALL || LIST_FOR_EACH
pTask = NULL;
task = &init_task;
pos = NULL;
printk(KERN_ALERT "list_for_each");
list_for_each(pos, &task->tasks)
{
pTask = list_entry(pos, struct task_struct, tasks );
if(pTask->pid == (pid_t)pid)
{
printk(KERN_ALERT"PID\tCOMM\tADDR\n");
printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask);
break;
}
}
#endif
#if LIST_ALL || FOR_EACH_PROCESS
pTask = NULL;
task = &init_task;
printk(KERN_ALERT "for_each_process");
for_each_process(task)
{
if(task->pid == pid)
{
printk(KERN_ALERT"PID\tCOMM\tADDR\n");
printk(KERN_ALERT "%d\t%s\t%p\n", task->pid, task->comm, task);
break;
}
}
#endif
#if LIST_ALL || LIST_FOR_EACH_ENTRY
pTask = NULL;
task = &init_task;
printk(KERN_ALERT "list_for_each_entry");
list_for_each_entry(pTask, &task->tasks, tasks)
{
if(pTask->pid == (pid_t)pid)
{
printk(KERN_ALERT"PID\tCOMM\tADDR\n");
printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask);
break;
}
}
#endif
return pTask;
}
static int init_find_task(void)
{
struct task_struct *pTask = NULL;
switch(METHOD)
{
case 1 :
{
pTask = find_task_by_pid_in_ns(PID);
break;
}
case 2 :
{
pTask = find_task_by_pid_in_list(PID);
break;
}
case 3 :
{
pTask = find_task_by_pid_in_rbtree(PID);
break;
}
}
print_task(pTask);
return 0;
}
static void exit_find_task(void)
{
printk(KERN_ALERT "GOOD BYE:find_task!!\n");
}
module_init(init_find_task);
module_exit(exit_find_task);
MODULE_AUTHOR("gatieme");
MODULE_LICENSE("GPL");
@@ -0,0 +1,27 @@
/*************************************************************************
> File Name: get_task.c
> Author: gatieme
> Created Time: Mon 16 May 2016 03:28:46 PM CST
************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include "print_vmarea.h"
void print_task(struct task_struct *task)
{
printk("process : %s, pid : %d\n", task->comm, task->pid);
print_task_vmarea(task);
}
@@ -0,0 +1,22 @@
#ifndef __PRINT_TASK_H_INCLUDE__
#define __PRINT_TASK_H_INCLUDE__
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include "print_vmarea.h"
void print_task(struct task_struct *task);
#endif // #define __PRINT_TASK_H_INCLUDE__
@@ -0,0 +1,82 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#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 void print_vmarea(struct vm_area_struct *vmarea)
{
printk("0x%lx - 0x%lx\t", vmarea->vm_start, vmarea->vm_end);
((vmarea->vm_flags & VM_READ) == 1) ? printk("r") : printk("-");
((vmarea->vm_flags & VM_WRITE) == 1) ? printk("w") : printk("-");
((vmarea->vm_flags & VM_EXEC) == 1) ? printk("x") : printk("-");
((vmarea->vm_flags & VM_SHARED) == 1) ? printk("s") : printk("p");
printk("\n");
}
static void print_vmarea_by_list(struct task_struct *task)
{
struct vm_area_struct *vmarea = task->mm->mmap;
printk("process : %s, pid : %d\n", task->comm, task->pid);
while (vmarea != NULL)
{
print_vmarea(vmarea);
vmarea = vmarea->vm_next;
}
}
static void visit_rbtree_node(struct rb_node *root)
{
struct vm_area_struct *vmarea = NULL;
vmarea = container_of(root,struct vm_area_struct,vm_rb);
print_vmarea(vmarea);
}
static void preorder_rbtree(struct rb_node *root)
{
if (root != NULL)
{
visit_rbtree_node(root);
preorder_rbtree(root->rb_left);
preorder_rbtree(root->rb_right);
}
}
static void print_vmarea_by_rbtree(struct task_struct *task)
{
struct rb_node *root = NULL;
root = task->mm->mm_rb.rb_node;
preorder_rbtree(root);
}
void print_task_vmarea(struct task_struct *task)
{
printk(KERN_ALERT "print vmarea_list");
print_vmarea_by_list(task);
printk(KERN_ALERT "print vmarea_rbtree");
print_vmarea_by_rbtree(task);
}
@@ -0,0 +1,9 @@
#ifndef __PRINT_VMAREA_H_INCLUDE__
#define __PRINT_VMAREA_H_INCLUDE__
void print_task_vmarea(struct task_struct *task);
#endif // #define __PRINT_VMAREA_H_INCLUDE__