c - 关于/proc/xx/map 和 vm_area_struct

标签 c linux linux-kernel kernel

内核模块代码:

static int __init module(void)
{
    struct pid *current_pid;
    struct task_struct *current_task;
    struct mm_struct  *mymm;
    struct vm_area_struct *pos = NULL;

    current_pid = find_vpid(7887);
    current_task = pid_task(current_pid, PIDTYPE_PID);
    mymm = current_task->mm;
    for(pos = mymm->mmap; pos; pos = pos->vm_next) {
        printk("0x%hx-0x%hx\t%ld\t", pos->vm_start, pos->vm_end,pos->rb_subtree_gap);
        if(pos->vm_flags & VM_READ) {
            printk("r");
        } else {
            printk("-");
        }
        if(pos->vm_flags & VM_WRITE) {
            printk("w");
        } else {
            printk("-");
        }
        if(pos->vm_flags & VM_EXEC) {
            printk("x");
        } else {
            printk("-");
        }

        printk("\n");
    }
    return 0;
}

static void __exit rmodule(void)
{
    printk(KERN_ALERT"Goodbye,world\n");
}

module_init(module);
module_exit(rmodule);

结果:

Oct  9 18:57:43 shui kernel: [14625.861352] 0x400000-0x401000   4194304 r-x
Oct  9 18:57:43 shui kernel: [14625.861366] 0x600000-0x601000   273183399936    r--
Oct  9 18:57:43 shui kernel: [14625.861373] 0x601000-0x602000   0   rw-
Oct  9 18:57:43 shui kernel: [14625.861379] 0x3f9b600000-0x3f9b620000   273183399936    r-x
Oct  9 18:57:43 shui kernel: [14625.861386] 0x3f9b820000-0x3f9b821000   140214666838016 r--
Oct  9 18:57:43 shui kernel: [14625.861391] 0x3f9b821000-0x3f9b822000   0   rw-
Oct  9 18:57:43 shui kernel: [14625.861397] 0x3f9b822000-0x3f9b823000   0   rw-
Oct  9 18:57:43 shui kernel: [14625.861403] 0x3f9ba00000-0x3f9bbad000   1953792 r-x
Oct  9 18:57:43 shui kernel: [14625.861408] 0x3f9bbad000-0x3f9bdad000   0   ---
Oct  9 18:57:43 shui kernel: [14625.861414] 0x3f9bdad000-0x3f9bdb1000   140214666838016 r--
Oct  9 18:57:43 shui kernel: [14625.861419] 0x3f9bdb1000-0x3f9bdb3000   0   rw-
Oct  9 18:57:43 shui kernel: [14625.861425] 0x3f9bdb3000-0x3f9bdb8000   140214666838016 rw-
Oct  9 18:57:43 shui kernel: [14625.861431] 0x7fc5e1444000-0x7fc5e1447000   140214666838016 rw-
Oct  9 18:57:43 shui kernel: [14625.861436] 0x7fc5e146f000-0x7fc5e1470000   140214666838016 rw-
Oct  9 18:57:43 shui kernel: [14625.861442] 0x7fffe4c32000-0x7fffe4c54000   249166569472    rw-
Oct  9 18:57:43 shui kernel: [14625.861448] 0x7fffe4d79000-0x7fffe4d7b000   1200128 r-x

但使用 cat/proc/7887/maps:

shui @ kernel @ 19:00]$ cat /proc/7887/maps 
00400000-00401000 r-xp 00000000 00:20 1826951                            /tmp/a.out

00600000-00601000 r--p 00000000 00:20 1826951                            /tmp/a.out

00601000-00602000 rw-p 00001000 00:20 1826951                            /tmp/a.out

3f9b600000-3f9b620000 r-xp 00000000 fd:01 2228400                        /usr/lib64
/ld-2.16.so

3f9b820000-3f9b821000 r--p 00020000 fd:01 2228400                        /usr/lib64/ld-2.16.so

3f9b821000-3f9b822000 rw-p 00021000 fd:01 2228400                        /usr/lib64/ld-2.16.so

3f9b822000-3f9b823000 rw-p 00000000 00:00 0 

3f9ba00000-3f9bbad000 r-xp 00000000 fd:01 2228403                        /usr/lib64/libc-2.16.so

3f9bbad000-3f9bdad000 ---p 001ad000 fd:01 2228403                        /usr/lib64/libc-2.16.so

3f9bdad000-3f9bdb1000 r--p 001ad000 fd:01 2228403                        /usr/lib64/libc-2.16.so

3f9bdb1000-3f9bdb3000 rw-p 001b1000 fd:01 2228403                        /usr/lib64/libc-2.16.so

3f9bdb3000-3f9bdb8000 rw-p 00000000 00:00 0 

7fc5e1444000-7fc5e1447000 rw-p 00000000 00:00 0 

7fc5e146f000-7fc5e1470000 rw-p 00000000 00:00 0 

7fffe4c33000-7fffe4c54000 rw-p 00000000 00:00 0                          [stack]

7fffe4d79000-7fffe4d7b000 r-xp 00000000 00:00 0                          [vdso]

ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

关于堆栈地址的vm_area_struct是7fffe4c32000,但是映射是7fffe4c33000,这有什么不同? vsyscall 是为什么不在 vm_area_struct 上打印?

最佳答案

这是正常的,Linux 内核使用堆栈保护,procfs 不显示页面。

查看fs/proc/task_mmu.c中的函数show_map_vma

关于c - 关于/proc/xx/map 和 vm_area_struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277134/

相关文章:

更改我在 C 函数中传递的指针

c++ - 对齐指针操作

在运行 amazon linux os 的 php7 上安装 php-bcmath

windows - 创建嵌套 opengl Canvas 的最佳方法是什么

linux - 显示名称包含空格的文件大小

linux - Linux 用户空间上的 USB 插件检测

linux - 进程死后,Linux 内核在哪里进行进程和 TCP 连接清理?

c - 服务器没有在预期的位置监听

c - 如何使用指针在数组中导航?

makefile - 如何在 makefile 中为内核模块构建目标设置预处理器指令?