Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1438664
  • 博文数量: 704
  • 博客积分: 10140
  • 博客等级: 上将
  • 技术积分: 6230
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-15 20:41
文章分类

全部博文(704)

文章存档

2013年(1)

2012年(16)

2011年(536)

2010年(151)

分类: C/C++

2011-06-23 10:35:02

进程的地址空间信息
下面的代码从进程描述符中,找出进程的地址空间信息

mm.c

#include <asm/processor.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/mm_types.h>
#include <asm/desc.h>
#include <asm/unistd.h>

int init_module(void)
{
    unsigned long cr3; 
    struct vm_area_struct *vm;
#if 0
    __asm__ __volatile__ {
        "movl %%cr3, %0\n\t"
            :"=r"(cr3):
    };
#endif
    vm = current->mm->mmap;

    printk("*****可用下面的方法取出进程的地址空间信息*******\n");
    printk("current->comm: %s\n", current->comm); //执行当前进程的命令的名字

    printk("current->state: %d\n", current->state); //运行状态

    printk("currentr->mm = 0x%x\n", current->mm); 
    printk("currentr->mm->pgd = 0x%x\n", current->mm->pgd); //页目录表的地址(好象与cr3相差0xc0000000)

    printk("**********the virtual memory area***************\n");
    printk("vm_area: 0x%x - 0x%x\n", current->mm->mmap->vm_start, current->mm->mmap->vm_end); //

    vm = vm->vm_next;
    while((NULL != vm) && (vm != current->mm->mmap)){
        printk("vm_area: 0x%x - 0x%x\n", vm->vm_start, vm->vm_end); //
        vm = vm->vm_next;
    };

    printk("total_vm: 0x%x\t locked_vm: 0x%x\t shared_vm: 0x%x\t exec_vm: 0x%x\n", 
            current->mm->total_vm, //进程地址空间的大小(页)
            current->mm->locked_vm, //锁住不能换出的页的个数
            current->mm->shared_vm, //共亨文件内存映射的页数
            current->mm->exec_vm); //可执行内存映射中的页数
    printk("start_code - end_code: 0x%x - 0x%x\n", 
            current->mm->start_code, 
            current->mm->end_code); //代码
    printk("start_data - end_data: 0x%x - 0x%x\n", 
            current->mm->start_data, 
            current->mm->end_data); //数据
    printk("start_brk - brk: 0x%x - 0x%x\n", 
            current->mm->start_brk, 
            current->mm->brk); //堆
    printk("start_stack: 0x%x\n", current->mm->start_stack); //用户堆栈的起始地址
    printk("stack_vm: 0x%x\n", current->mm->stack_vm); //用户态堆栈中的页数
    printk("arg_area: 0x%x - 0x%x\n", current->mm->arg_start, current->mm->arg_end); //命令行参数的起始地址
    printk("env_area: 0x%x - 0x%x\n", current->mm->env_start, current->mm->env_end); //环境变量的起始地址

    return 0;
}

void cleanup_module(void)
{
    return;
}


下面是相应的Makefile文件

ifneq ($(KERNELRELEASE),)
                obj-:= mm.o
else
                KERNELDIR := /lib/modules/$(shell uname -r)/build
                PWD := $(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

clean:
        rm -*.ko *..*.cmd .tmp_versions *.mod.*.symvers modules.order


执行make命令后生成mm.ko文件
执行"sudo insmod mm.ko"命令插入模块
执行cat /var/log/messages(或dmesg)命令查看输出的进程地址空间信息。

下面是输出的进程地址空间信息。进程空间的使用很清楚了:
Mar 23 00:58:59 zhanglong kernel: [17044.195061] *****可用下面的方法取出进程的地址空间信息*******
Mar 23 00:58:59 zhanglong kernel: [17044.195065] current->comm: insmod
Mar 23 00:58:59 zhanglong kernel: [17044.195066] current->state: 0
Mar 23 00:58:59 zhanglong kernel: [17044.195068] currentr->mm = 0xf47b56c0
Mar 23 00:58:59 zhanglong kernel: [17044.195069] currentr->mm->pgd = 0xd4576000
Mar 23 00:58:59 zhanglong kernel: [17044.195070] **********the virtual memory area***************
Mar 23 00:58:59 zhanglong kernel: [17044.195072] vm_area: 0xe07000 - 0xe22000
Mar 23 00:58:59 zhanglong kernel: [17044.195073] vm_area: 0xe22000 - 0xe23000
Mar 23 00:58:59 zhanglong kernel: [17044.195074] vm_area: 0xe23000 - 0xe24000
Mar 23 00:58:59 zhanglong kernel: [17044.195076] vm_area: 0xe61000 - 0xfb4000
Mar 23 00:58:59 zhanglong kernel: [17044.195077] vm_area: 0xfb4000 - 0xfb5000
Mar 23 00:58:59 zhanglong kernel: [17044.195078] vm_area: 0xfb5000 - 0xfb7000
Mar 23 00:58:59 zhanglong kernel: [17044.195079] vm_area: 0xfb7000 - 0xfb8000
Mar 23 00:58:59 zhanglong kernel: [17044.195080] vm_area: 0xfb8000 - 0xfbb000
Mar 23 00:58:59 zhanglong kernel: [17044.195082] vm_area: 0xfca000 - 0xfcb000
Mar 23 00:58:59 zhanglong kernel: [17044.195083] vm_area: 0x8048000 - 0x804a000
Mar 23 00:58:59 zhanglong kernel: [17044.195084] vm_area: 0x804a000 - 0x804b000
Mar 23 00:58:59 zhanglong kernel: [17044.195085] vm_area: 0x804b000 - 0x804c000
Mar 23 00:58:59 zhanglong kernel: [17044.195087] vm_area: 0x8f32000 - 0x8f53000
Mar 23 00:58:59 zhanglong kernel: [17044.195088] vm_area: 0xb7721000 - 0xb7722000
Mar 23 00:58:59 zhanglong kernel: [17044.195089] vm_area: 0xb7735000 - 0xb7737000
Mar 23 00:58:59 zhanglong kernel: [17044.195090] vm_area: 0xbf8fd000 - 0xbf913000
Mar 23 00:58:59 zhanglong kernel: [17044.195092] total_vm: 0x1b6     locked_vm: 0x0     shared_vm: 0x178     exec_vm: 0x170
Mar 23 00:58:59 zhanglong kernel: [17044.195093] start_code - end_code: 0x8048000 - 0x8049434
Mar 23 00:58:59 zhanglong kernel: [17044.195095] start_data - end_data: 0x804ae94 - 0x804b008
Mar 23 00:58:59 zhanglong kernel: [17044.195096] start_brk - brk: 0x8f32000 - 0x8f53000
Mar 23 00:58:59 zhanglong kernel: [17044.195097] start_stack: 0xbf910a00
Mar 23 00:58:59 zhanglong kernel: [17044.195098] stack_vm: 0x16
Mar 23 00:58:59 zhanglong kernel: [17044.195100] arg_area: 0xbf9129dd - 0xbf9129ea
Mar 23 00:58:59 zhanglong kernel: [17044.195101] env_area: 0xbf9129ea - 0xbf912fef
阅读(643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~