Linux获取进程中变量

列出所有进程

 1 #include <linux/kernel.h>
 2 #include <linux/module.h>
 3 #include <linux/init.h>
 4 #include <linux/sched.h>
 5 #include <linux/list.h>
 6 
 7 static __init int print_pid(void)
 8 {
 9     struct task_struct *task,*p;
10     struct list_head *pos;
11     int count=0;
12     printk("Hello,let begin
");
13     task = &init_task;
14     list_for_each(pos,&task->tasks)
15     {
16         p = list_entry(pos, struct task_struct, tasks);
17         count++;
18         printk("%d---->%s-->%X
",p->pid,p->comm, p->state);
19     }
20     printk("the number of process is:%d
",count);
21     return 0;
22 }
23 
24 static __exit void print_exit(void)
25 {
26     printk("<0>end!
");
27 }
28 module_init(print_pid);
29 module_exit(print_exit);
View Code

 获得虚拟地址的物理内存

 1 /*
 2 
 3 *伪代码,示例
 4 
 5 *32位地址,三级映射(没有pud_t),页面大小4KB
 6 
 7 */
 8 
 9 unsigned long addr = 0x12345678;//要找的虚拟地址,用户空间所访问的地址
10 
11 unsigned long real_addr = 0x00;//要输出的地址
12 
13 struct task_struct *cur_task = get_current();//获取当前进程控制块
14 
15 struct mm_struct *mm = cur_task ->  mm;//进程虚拟空间
16 
17 pgd_t *pgd;//描述页全局目录项
18 
19 pmd_t *pmd;//描述页中间项
20 
21 pte_t *pte;//页表项
22 
23 
24 
25 pgd = pgd_offset(mm, addr);//找出所在目录
26 
27 if (pgd_none(*pgd)){
28 
29         goto out;
30 
31 }
32 
33 pmd = pmd_offset(pgd, addr);//找出所在中间项
34 
35 
36 
37 if (pmd_none(*pmd)){
38 
39     goto out;
40 
41 }
42 
43 pte = pte_offset(pmd, addr);//找出所在页面
44 
45 
46 
47 
48 
49 if (pte_none(*pte)) {
50 
51     goto out;
52 
53 }
54 
55 
56 
57 //假设每页4KB
58 
59 real_addr = addr & 0x00003fff; //取出页面偏移量
60 
61 real_addr += pte;//内核空间访问的地址
62 
63 real_addr -= PAGE_OFFSET;//真正物理地址()
64 
65 printk("物理地址是 %x
",real_addr);
66 
67 return;
68 
69 
70 
71 out:
72 
73 printk("没有内存映射",real_addr);
View Code
原文地址:https://www.cnblogs.com/macinchang/p/4591918.html