-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpid3.c
223 lines (210 loc) · 4.69 KB
/
pid3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <asm/page.h>
#include <asm/pgtable.h>
MODULE_AUTHOR("Zhongqiu Wang");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A module to get all the physical address...");
static unsigned long v2p(struct mm_struct *mm, unsigned long addr)
{
unsigned long ret = 1;
pgd_t *pgd;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
spinlock_t *ptl;
pgd = pgd_offset(mm, addr);
if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
{
goto out;
// printk("pgd not ok\n");
}
pud = pud_offset(pgd, addr);
if (pud_none(*pud) || unlikely(pud_bad(*pud)))
{
//j printk("pud not ok\n");
goto out;
}
pmd = pmd_offset(pud, addr);
if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
{
// printk("pmd not ok\n");
goto out;
}
pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
if (!pte)
{
// printk("pte not ok\n");
goto out;
}
if (!pte_present(*pte))
{
// printk("pte_present not ok\n");
goto unlock;
}
ret = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
unlock:
pte_unmap_unlock(pte, ptl);
out:
return ret;
}
int flag = 0;
int pids[2048];
int number_of_process = 0;
int count = 0;
int count2 = 0;
static void *ps_seq_start(struct seq_file *s,loff_t *pos)
{
printk("start pos is %d\n",*pos);
if(*pos == 0)
{
printk("************Let's Begin**************\n");
struct task_struct *temp_task = &init_task;
number_of_process = 0;
do
{
printk("%d\n",temp_task->pid);
pids[number_of_process++] = temp_task->pid;
}while((temp_task = next_task(temp_task)) != &init_task);
printk("number of process %d\n",number_of_process);
}
if(*pos > number_of_process)
{
return NULL;
}
else
{
struct task_struct *p;
struct pid *k;
do
{
if(*pos >= number_of_process)
return NULL;
k = find_vpid(pids[(*pos)++]);
p = pid_task(k,PIDTYPE_PID);
if(p == NULL)
{
printk("task->pid = %d not found\n",pids[*pos - 1]);
}
}while(p == NULL);
return p;
}
}
static void *ps_seq_next(struct seq_file *s,void *v,loff_t *pos)
{
printk("next\n");
//struct task_struct *task=(struct task_struct *)v;
struct task_struct *p;
struct pid *k;
if(*pos > number_of_process)
{
return NULL;
}
do
{
if(*pos >= number_of_process)
return NULL;
k = find_vpid(pids[(*pos)++]);
p = pid_task(k,PIDTYPE_PID);
if(p == NULL)
{
printk("task->pid = %d not found\n",pids[*pos - 1]);
}
}while(p == NULL);
return p;
}
static void ps_seq_stop(struct seq_file *s,void *v)
{
printk("stop\n");
}
static int ps_seq_show(struct seq_file *s,void *v)
{
rwlock_t lock = RW_LOCK_UNLOCKED;
struct task_struct *task=(struct task_struct *)v;
struct vm_area_struct * temp = NULL;
printk("show\n");
if(task == NULL)
{
printk("task is null\n");
return 0;
}
if(!task->mm)
{
printk("task->pid = %d don't have mm\n",task->pid);
return 0;
}
else
{
temp = task->mm->mmap;
}
read_lock(&lock);
seq_printf(s,"%d %d %s#",task->pid,task->parent->pid,task->comm);
printk("task->pid = %d OOOOOOOO\n",task->pid);
while(temp)
{
unsigned long first = ((temp->vm_start) >> 12),second = ((temp->vm_end) >> 12),count,num;
for(count = first;count < second;count++)
{
//获得对应的祯
num = v2p(task->mm,count << 12);
if(num == 1)
continue;
num = (num >> 12);
seq_printf(s,"%lu,",num);
}
temp = temp->vm_next;
}
seq_printf(s,"|");
read_unlock(&lock);
return 0;
}
static struct seq_operations ps_seq_ops = {
.start = ps_seq_start,
.next = ps_seq_next,
.stop = ps_seq_stop,
.show = ps_seq_show
};
static int ps_open(struct inode *inode,struct file *file)
{
struct seq_file *p = file->private_data;/*p为seq_file结构实例*/
if (!p)
{
p = kmalloc(sizeof(*p) , GFP_KERNEL);
if (!p)
return -ENOMEM;
file->private_data = p;/*放到file的private_data中*/
}
memset(p, 0, sizeof(*p));
mutex_init(&p->lock);
p->op = &ps_seq_ops;/*设置seq_file的operation为op*/
file->f_version = 0;
file->f_mode &= ~FMODE_PWRITE;
return 0;
}
static struct file_operations ps_file_ops = {
.owner = THIS_MODULE,
.open = ps_open,
.read = seq_read,
.llseek = seq_lseek,
.release= seq_release
};
static int __init ps_init(void)
{
struct proc_dir_entry *entry;
entry = create_proc_entry("mymemory",0,NULL);
if(entry)
entry->proc_fops = &ps_file_ops;
return 0;
}
static void __exit ps_exit(void)
{
remove_proc_entry("mymemory",NULL);
}
module_init(ps_init);
module_exit(ps_exit);